1

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

2
  • Is the input array sorted? And what have you tried so far in terms of recursion? Commented Sep 8, 2021 at 19:48
  • Yes the depth variable is sorted. I've tried several recursive methods unsuccessfully, but ive spent a lot of time to make it work, and was curious if anyone else has tackled something similar Commented Sep 8, 2021 at 19:51

2 Answers 2

2

It's easier to create a tree when using an Map or a dictionary (object) to retrieve parents. After reducing the entire tree to a Map, retrieve the root, which holds the entire tree.

const createItem = (type) => ({ type, children: [] })

const fn = (arr, root) => arr.reduce((acc, d) => {  
  const parent = d - 1
  
  const item = createItem(d)
  
  if(d !== root && !acc.has(parent)) acc.set(parent, createItem(parent))
  
  if(d !== root) acc.get(parent).children.push(item)
  
  return acc.set(d, item);
}, new Map()).get(root);

const depth = [0, 1, 2, 3, 3, 2, 3, 3, 3 ]

const result = fn(depth, 0)

console.log(result)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer - this definitely gives me another perspective on how to solve this!
1

As you get the data in pre-order sequence, you don't actually need a map:

function convert(data) {
    let i = 0;
    
    function getChildren(type) {
        let children = [];
        while (data[i] === type) {
            i++;
            children.push({
                type,
                children: getChildren(type + 1)
            });
        }
        return children;
    }
    
    return getChildren(0);
}

let data = [0, 1, 2, 3, 3, 2, 3, 3, 3];
console.log(convert(data));

1 Comment

Thank you for the answer, this solved my issue and i think for my use-case is the best answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.