Converting tree data in to flat data with hierarchy path as an array

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 ?

2 answers

  • answered 2018-10-11 19:56 Nenad Vracar

    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))

  • answered 2018-10-11 19:56 Callam

    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]))