如何循环遍历Javascript对象数组,将对象分配为其他对象的子对象?树/层次结构

时间:2018-02-22 22:07:19

标签: javascript arrays loops tree

我需要帮助循环遍历以下对象数组,以将所有内容重新组织为层次结构/树。我该如何实现这一目标?任何帮助都表示赞赏。

[  
   {  
      "id":1,
      "parentId":null,
      "value":"Root Parent 01",
      "children":[]
   },
   {  
      "id":8,
      "parentId":null,
      "value":"Root Parent 02",
      "children":[]
   },
   {  
      "id":36,
      "parentId":15,
      "value":"Child 01",
      "children":[]
   },
   {  
      "id":43,
      "parentId":15,
      "value":"Child 02",
      "children":[]

   },
   {  
      "id":50,
      "parentId":15,
      "value":"Child 03",
      "children":[]

   },
   {  
      "id":15,
      "parentId":null,
      "value":"Root Parent 03",
      "children":[]
   },
   {  
      "id":57,
      "parentId":22,
      "value":"Child 04",
      "children":[]

   },
   {  
      "id":64,
      "parentId":22,
      "value":"Child 05",
      "children":[]

   },
   {  
      "id":71,
      "parentId":22,
      "value":"Child 06",
      "children":[]

   },
   {  
      "id":78,
      "parentId":22,
      "value":"Child 07",
      "children":[]

   },
   {  
      "id":85,
      "parentId":22,
      "value":"Child 08",
      "children":[]

   },
   {  
      "id":92,
      "parentId":22,
      "value":"Child 09",
      "children":[]
   },
   {  
      "id":99,
      "parentId":22,
      "value":"Child 10",
      "children":[]

   },
   {  
      "id":106,
      "parentId":22,
      "value":"Child 11",
      "children":[]

   },
   {  
      "id":22,
      "parentId":null,
      "value":"Root Parent 04",
      "children":[]
   },
   {  
      "id":113,
      "parentId":29,
      "value":"Child 12",
      "children":[]

   },
   {  
      "id":115,
      "parentId":29,
      "value":"Child 13",
      "children":[]

   },
   {  
      "id":122,
      "parentId":29,
      "value":"Child 14",
      "children":[]

   },
   {  
      "id":129,
      "parentId":29,
      "value":"Child 15",
      "children":[]

   },
   {  
      "id":136,
      "parentId":29,
      "value":"Child 16",
      "children":[]

   },
   {  
      "id":143,
      "parentId":29,
      "value":"Child 17",
      "children":[]

   },
   {  
      "id":157,
      "parentId":150,
      "value":"Child 18",
      "children":[]

   },
   {  
      "id":150,
      "parentId":29,
      "value":"Child 19",
      "children":[]

   },
   {  
      "id":29,
      "parentId":null,
      "value":"Root Parent 05",
      "children":[]
   }
]

所需的对象数组:

[
    {  
        "id":1,
        "parentId":null,
        "value":"Root Parent 01",
        "children":[]
    },
    {  
        "id":8,
        "parentId":null,
        "value":"Root Parent 02",
        "children":[]
    },
    {  
        "id":15,
        "parentId":null,
        "value":"Root Parent 03",
        "children":[
            {  
                "id":36,
                "parentId":15,
                "value":"Child 01",
                "children":[]
            },
            {
                "id":43,
                "parentId":15,
                "value":"Child 02",
                "children":[]
            },
            {
                "id":50,
                "parentId":15,
                "value":"Child 03",
                "children":[]
            }
        ]
    },
    {  
        "id":22,
        "parentId":null,
        "value":"Root Parent 04",
        "children":[
            {
                "id":57,
                "parentId":22,
                "value":"Child 04",
                "children":[]
            },
            {
                "id":64,
                "parentId":22,
                "value":"Child 05",
                "children":[]
            },
            {
                "id":71,
                "parentId":22,
                "value":"Child 06",
                "children":[]
            },
            {
                "id":78,
                "parentId":22,
                "value":"Child 07",
                "children":[]
            },
            {
                "id":85,
                "parentId":22,
                "value":"Child 08",
                "children":[]
            },
            {
                "id":92,
                "parentId":22,
                "value":"Child 09",
                "children":[]
            },
            {
                "id":99,
                "parentId":22,
                "value":"Child 10",
                "children":[]
            },
            {
                "id":106,
                "parentId":22,
                "value":"Child 11",
                "children":[]
            }
        ]
    },
    {  
        "id":29,
        "parentId":null,
        "value":"Root Parent 05",
        "children":[
            {
                "id":113,
                "parentId":29,
                "value":"Child 12",
                "children":[]
            },
            {
                "id":115,
                "parentId":29,
                "value":"Child 13",
                "children":[]
            },
            {
                "id":122,
                "parentId":29,
                "value":"Child 14",
                "children":[]
            },
            {
                "id":129,
                "parentId":29,
                "value":"Child 15",
                "children":[]
            },
            {
                "id":136,
                "parentId":29,
                "value":"Child 16",
                "children":[]
            },
            {
                "id":143,
                "parentId":29,
                "value":"Child 17",
                "children":[]
            },
            {
                "id":150,
                "parentId":29,
                "value":"Child 19",
                "children":[
                    {
                        "id":157,
                        "parentId":150,
                        "value":"Child 18",
                        "children":[]
                    }
                ]
            }
        ]
    }
]

所需树

Root Parent 01
根父2 02 Root Parent 03
---孩子01
---孩子02
---孩子03
根父母04
---孩子04
---孩子05
---孩子06
---儿童07
---孩子08
---儿童09
---孩子10
---孩子11
根父母05
---孩子12
---孩子13
---孩子14
---孩子15
---孩子16
---孩子17
---孩子19
------孩子18岁

1 个答案:

答案 0 :(得分:1)

虽然我是一个尝试自己尝试的粉丝。我一直想知道如何做到这一点,并认为这对我来说也是一个很好的机会。

首先,我对输入列表进行排序,以查找肯定是子元素而不是父元素的项目。

然后在第二个循环中,我们寻找孩子所属的父母,然后我们匹配它们。您可能需要根据数据优化此过程。你可能还需要循环多次,但这应该足以让你有所作为。



var inputArray = [{
    "id": 1,
    "parentId": null,
    "value": "Root Parent 01",
    "children": []
  },
  {
    "id": 8,
    "parentId": null,
    "value": "Root Parent 02",
    "children": []
  },
  {
    "id": 36,
    "parentId": 15,
    "value": "Child 01",
    "children": []
  },
  {
    "id": 43,
    "parentId": 15,
    "value": "Child 02",
    "children": []

  },
  {
    "id": 50,
    "parentId": 15,
    "value": "Child 03",
    "children": []

  },
  {
    "id": 15,
    "parentId": null,
    "value": "Root Parent 03",
    "children": []
  },
  {
    "id": 57,
    "parentId": 22,
    "value": "Child 04",
    "children": []

  },
  {
    "id": 64,
    "parentId": 22,
    "value": "Child 05",
    "children": []

  },
  {
    "id": 71,
    "parentId": 22,
    "value": "Child 06",
    "children": []

  },
  {
    "id": 78,
    "parentId": 22,
    "value": "Child 07",
    "children": []

  },
  {
    "id": 85,
    "parentId": 22,
    "value": "Child 08",
    "children": []

  },
  {
    "id": 92,
    "parentId": 22,
    "value": "Child 09",
    "children": []
  },
  {
    "id": 99,
    "parentId": 22,
    "value": "Child 10",
    "children": []

  },
  {
    "id": 106,
    "parentId": 22,
    "value": "Child 11",
    "children": []

  },
  {
    "id": 22,
    "parentId": null,
    "value": "Root Parent 04",
    "children": []
  },
  {
    "id": 113,
    "parentId": 29,
    "value": "Child 12",
    "children": []

  },
  {
    "id": 115,
    "parentId": 29,
    "value": "Child 13",
    "children": []

  },
  {
    "id": 122,
    "parentId": 29,
    "value": "Child 14",
    "children": []

  },
  {
    "id": 129,
    "parentId": 29,
    "value": "Child 15",
    "children": []

  },
  {
    "id": 136,
    "parentId": 29,
    "value": "Child 16",
    "children": []

  },
  {
    "id": 143,
    "parentId": 29,
    "value": "Child 17",
    "children": []

  },
  {
    "id": 157,
    "parentId": 150,
    "value": "Child 18",
    "children": []

  },
  {
    "id": 150,
    "parentId": 29,
    "value": "Child 19",
    "children": []

  },
  {
    "id": 29,
    "parentId": null,
    "value": "Root Parent 05",
    "children": []
  }
]

function DataRollUp(input) {
  var hasParent = [];
  var output = []
  for (i of input) {
    if (i.parentId) {
      hasParent.push(i);
    } else {
      output.push(i);
    }
  }

  for (i of hasParent) {
    var parent = output.find(record => record.id == i.parentId)

    if (parent) {
      parent.children.push(i);
    }
  }


  console.log(output);
  return output;

}

DataRollUp(inputArray)