【问题标题】:Converting tree data in to flat data with hierarchy path as an array将树数据转换为具有层次结构路径的平面数据作为数组
【发布时间】:2019-03-16 23:32:02
【问题描述】:

我有以下数据。我需要创建一个平面数据结构,其层次结构作为一个数组。如何使用递归函数创建它?

    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
        },
    ]
}];

我想要的结果应该是这样的。

[{
            "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]
        }
    ]  

谁能帮忙?

【问题讨论】:

  • 到目前为止你有什么尝试?

标签: javascript angular typescript data-structures


【解决方案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]))

【讨论】:

    【解决方案2】:

    您可以使用reduce 方法执行此操作并创建一个递归函数。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-08
      • 2020-11-30
      • 1970-01-01
      • 2020-05-22
      • 2023-01-13
      • 2015-09-25
      • 1970-01-01
      • 2018-02-06
      相关资源
      最近更新 更多