I'm having some issues translating a depth list into a nested object.
For example, i have a list like so:
"depth": [ 0, 1, 2, 3, 3, 2, 3, 3, 3 ],
And i need to come up with a recursive function to produce 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": []},
]
}
]
}
]
}
]
}
So the rule here, the lower numbers are parents, the higher numbers are siblings to the previous lower number.
So far what i have come up with is:
const depth = [0, 1, 2, 3, 3, 2, 3, 3, 3]
// Start looping through all the numbers in the depth
for (let i = 0; i < depth.length; i++) { //depth.length
// As i loop i want to only look at the array i have explored
// So that i can find the parent that is 1 lower in the array
let getParent = depth.slice(0, i).lastIndexOf(depth[i] - 1) // position of last lower array
// Here i check if the current depth item is bigger than the currently lower Item in the array
if (depth[i] > depth[getParent]) {
console.log(depth[i] + " Nesting into " + depth[getParent]) // Is child of that item
}
}
I think this successfully maps the child to the parent. But now I'm stuck on a way to produce my desired result.
If anyone has an suggestions I would appreciate it a lot.
Thanks