【问题标题】:How can I create nested tree in array object如何在数组对象中创建嵌套树
【发布时间】:2020-03-16 20:23:07
【问题描述】:

跟随数组对象数据: 使用 Javascript 数据结构,每次当我使用其 ID 和父 ID 获取新子对象时,我如何将数组对象迭代到子数组中。

尝试使用 javascript 查找父 ID 和子嵌套根节点,但我无法找到如何将数组对象迭代到其子数组中。谁能帮帮我。

var data = [ 
  { id: 7, label: 'a', parentId: 2, childerns: [] },
  { id: 7, label: 'm', parentId: 2, childerns: [] },
  { id: 2, label: 'b', parentId: 5, childerns: [] },
  { id: 5, label: 'c', parentId: 20, childerns: [] },
  { id: 20, label: 'x', parentId: null, childerns: [] },
  { id: 8, label: 'd', parentId: 7, childerns: [] },
  { id: 9, label: 'n', parentId: 8, childerns: [] },
  { id: 9, label: 'n', parentId: 8, childerns: [] } ];```

and looking for the following nested tree pattern:

```var data = [
    {
        id: 20,
        label: 'x',
        parentId: null,
        childerns: [
            {
                id: 5,
                label: 'c',
                parentId: 20,
                childerns: [{
                    id: 2,
                    label: 'b',
                    parentId: 5,
                    childerns: [
                        {
                            id: 7, 
                            label: 'm',
                            parentId: 2,
                            childerns: [] 
                        },
                        {
                            id: 7,
                            label: 'a',
                            parentId: 2,
                            childerns: [{
                                id: 8,
                                label: 'd',
                                parentId: 7,
                                childerns: [
                                    {
                                        id: 9,
                                        label: 'n',
                                        parentId: 8,
                                        childerns: []
                                    },
                                    {
                                        id: 9,
                                        label: 'n',
                                        parentId: 8,
                                        childerns: []
                                    }
                                ]
                            }]
                        }]
                }]
            }]
    }];```

【问题讨论】:

    标签: javascript arrays algorithm data-structures nested-loops


    【解决方案1】:

    我喜欢下面的模式。首先,您使用一个对象来跟踪每个元素在原始数组中的位置。然后,遍历原始数组,将当前元素的引用添加到其父数组。如果parentId 为空,则将该元素添加到roots。毕竟,您的 roots 数组将包含完整的树。

    const arr = [
      { id: 7, label: 'm', parentId: 2, childerns: [] },
      { id: 2, label: 'b', parentId: 5, childerns: [] },
      { id: 5, label: 'c', parentId: 20, childerns: [] },
      { id: 20, label: 'x', parentId: null, childerns: [] },
      { id: 8, label: 'd', parentId: 7, childerns: [] },
      { id: 9, label: 'n', parentId: 8, childerns: [] },
      { id: 10, label: 'n', parentId: 8, childerns: [] } 
    ];
    
    // Map element ID to arr index
    const arrMap = arr.reduce((acc, el, i) => {
      acc[el.id] = i;
      return acc;
    }, {});
    
    const roots = [];
    
    // Push each element to parent's children array
    arr.forEach(el => {
      if (el.parentId === null) {
        roots.push(el);
      } else {
        arr[arrMap[el.parentId]].childerns.push(el);
      }
    });
    
    console.log(roots);

    【讨论】:

    • 您缺少重复的数组对象,该对象被重新运行两次。喜欢{ id: 7, label: 'a', parentId: 2, childerns: [] }, { id: 7, label: 'm', parentId: 2, childerns: [] },
    • 我认为这是一个错误。数组对象重复时怎么办?
    • 应采取一切措施并创建嵌套数组。
    • 那么如果有两个ID相同的父母,孩子去哪里呢?在您的示例树中,为什么 ID 8 仅在 ID 7 节点之一下而不在另一个节点下?
    • 无论哪个副本最后出现,我们可以为其分配一个。
    【解决方案2】:

    如果您正在寻找子级几乎可以无限扩展的模式,您应该使用类或构造函数,这样您就可以在其中传递另一个实例,如下面的UnitInstance.addChild 方法所示。

     /* constructor */
    function Unit(id, label, children){
      this.id = id; this.label = label; this.parentId = null; this.children = [];
      var t = this;
      this.addChild = function(unitInstance){
        unitInstance.parentId = this.id; this.children.push(unitInstance);
        return this;
      }
      if(children){
        children.forEach(function(o){
          t.addChild(o);
        });
      }
      this.getData = function(){
        var c = [];
        this.children.forEach(function(o){
          c.push(o.getData());
        });
        return {id:t.id, label:t.label, parentId:t.parentId, children:c};
      }
    }
    var x = new Unit(20, 'x'), c = new Unit(5, 'c'), b = new Unit(2, 'b'), m = new Unit(7, 'm'); a = new Unit(7, 'a'), d = new Unit(8, 'd'), n = new Unit(9, 'n');
    x.addChild(c.addChild(b.addChild(m.addChild(a.addChild(d.addChild(n).addChild(n))))));
    var data = [x.getData()];
    console.log(data);

    【讨论】:

    • 每次收到新数据时都不能硬编码嵌套
    • 每次收到新数据时,创建一个新单元,您可以将UnitInstance.getData() 推送到您的数据数组中。
    • 如果你在活动中这样做,那么它就是动态的。
    猜你喜欢
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 2018-05-20
    相关资源
    最近更新 更多