0

I have the following data. I need to create a flat data structure with its hierarchy as an array. How can I create it with a recursive function?

    var tree = [{
    "name": "Firm",
    "id": 1,
    "children": [{
            "name": "veniam",
            "id": 2
        },
        {
            "name": "officia",
            "id": 3
        },
        {
            "name": "ullamco",
            "id": 4
        },
        {
            "name": "duis",
            "id": 5,
            "children": [{
                "name": "aliquip",
                "id": 6,
                "children": [
                    {
                        "name": "culpa",
                        "id": 7
                    },
                    {
                        "name": "qui",
                        "id": 8
                    },
                    {
                        "name": "cillum",
                        "id": 9
                    }
                ]
            }]
        },
        {
            "name": "ullamco",
            "id": 10
        },
    ]
}];

My desired result should look like this.

[{
            "name": "Firm",
            "id": 1,
            "path": [1]
        },
        {
            "name": "veniam",
            "id": 2,
            "path": [1, 2]
        },
        {
            "name": "officia",
            "id": 3,
            "path": [1, 3]
        },
        {
            "name": "ullamco",
            "id": 4,
            "path": [1, 4]
        },
        {
            "name": "duis",
            "id": 5,
            "path": [1, 5]
        },
        {
            "name": "aliquip",
            "id": 6,
            "path": [1, 5, 6]
        },
        ...
        {
            "name": "ullamco",
            "id": 10,
            "path": [1, 10]
        }
    ]  

Can anyone help ?

1
  • 1
    What have you tried so far? Commented Oct 11, 2018 at 19:37

2 Answers 2

1

var tree=[{name:"Firm",id:1,children:[{name:"veniam",id:2},{name:"officia",id:3},{name:"ullamco",id:4},{name:"duis",id:5,children:[{name:"aliquip",id:6,children:[{name:"culpa",id:7},{name:"qui",id:8},{name:"cillum",id:9}]}]},{name:"ullamco",id:10}]}];

function flatten(node, path = [], array = []) {
  const { id, name } = node
  const newPath = [...path, id]
  const children = node.children || []
  array.push({ id, name, path: newPath })
  children.forEach(child => {
    flatten(child, newPath, array)
  })
  return array
}

console.log(flatten(tree[0]))

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

Comments

1

You could do this using reduce method and create an recursive function.

var tree = [{"name":"Firm","id":1,"children":[{"name":"veniam","id":2},{"name":"officia","id":3},{"name":"ullamco","id":4},{"name":"duis","id":5,"children":[{"name":"aliquip","id":6,"children":[{"name":"culpa","id":7},{"name":"qui","id":8},{"name":"cillum","id":9}]}]},{"name":"ullamco","id":10}]}]

function flat(data, prev = []) {
  return data.reduce((r, {id, name, children}) => {
    let path = prev.concat(id)
    if(children) r.push(...flat(children, path));
    r.push({name, id, path});
    return r;
  }, [])
}

console.log(flat(tree))

Comments

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.