-
Notifications
You must be signed in to change notification settings - Fork 124
Description
@larsga I have another unique problem in dealing with nested structures and need your opinion. I have duplicate reference node, I need to figure out how to private the duplicate from being created. I started building a global array and thinking that I need to check that array in flattening to remove duplicates. If you can give it a look and let me know what you think the best option here is then it will be great. Below is current input, expected output, and the current template.
Input
[
{
"chapters": [
{
"name": "a-f"
},
{
"name": "b"
}
]
},
{
"chapters": [
{
"name": "a-f"
},
{
"name": "e"
}
]
},
{
"chapters": [
{
"name": "l-p"
},
{
"name": "m"
}
]
},
{
"chapters": [
{
"name": "v-z"
},
{
"name": "v"
}
]
},
{
"chapters": [
{
"name": "v-z"
},
{
"name": "z"
}
]
}
]
Expected Output
[
{
"name": "a-f"
},
{
"name": "b",
"parents": [
{
"name": "a-f"
}
]
},
{
"name": "e",
"parents": [
{
"name": "a-f"
}
]
},
{
"name": "l-p"
},
{
"name": "m",
"parents": [
{
"name": "l-p"
}
]
},
{
"name": "v-z"
},
{
"name": "v",
"parents": [
{
"name": "v-z"
}
]
},
{
"name": "z",
"parents": [
{
"name": "v-z"
}
]
}
]
Current Template
def flatten(arrayofarrays)
if ($arrayofarrays)
$arrayofarrays[0] + flatten($arrayofarrays[1 : ])
else
[]let globalTopNodeCache = [for (.) if(.chapters and size(.chapters) > 0) .chapters[0].name]
flatten([for (.)
let chapterZero = if(.chapters and size(.chapters) > 0) .chapters[0] else ""
[{"name" : $chapterZero .name}] + [for (.chapters[1 : ]) {"parents":[{"name":$chapterZero .name}],"name" : .name}]
])