Struggling with translating a depth list into a nested object.
Consider this list:
"depth": [ 0, 1, 2, 3, 3, 2, 3, 3, 3 ],
I'm trying to create a recursive function that generates an object like this:
"depth": [
{
"type": 0,
"children": [
{
"type": 1,
"children": [
{
"type": 2,
"children":[
{ "type": 3, "children": []},
{ "type": 3, "children": []},
]
},
{
"type:": 2,
"children":[
{ "type": 3, "children": []},
{ "type": 3, "children": []},
{ "type": 3, "children": []},
]
}
]
}
]
}
]
}
The lower numbers represent parents, while the higher numbers are siblings of the previous lower number.
I've managed to map children to parents in my code so far, but I'm struggling with generating the final result. Any suggestions would be greatly appreciated.
Thanks!