遍历/遍历每个子元素以添加/修改其属性

时间:2019-04-24 12:43:34

标签: javascript arrays object

我有一个场景需要遍历树结构的每个子元素并修改/添加属性或属性。每个孩子可以有多个孩子

      var treeStructure = {
        "id": 1,
        "name": "Grand Parent 1",
        "children": [
          {
            "id": 2,
            "children": [
              {
                "id": 3,
                "children": [],
                "name": "Child 11",
                "properties": [
                  {
                    "id": 15,
                    "run": "fast"
                  },
                  {
                    "id": 16,
                    "walk": "slow"
                  }
                ]
              },
              {
                "id": 4,
                "type": "Child",
                "children": [],
                "name": "Child 12",
                "properties": [
                  {
                    "id": 17,
                    "run": "slow"
                  },
                  {
                    "id": 18,
                    "walk": "medium"
                  }
                ]
              }
            ],
            "name": "Parent 1",
            "properties": [
              {
                "id": 12,
                "run": "slow"
              },
              {
                "id": 13,
                "walk": "fast"
              }
            ]
          },
          {
            "id": 5,
            "children": [
              {
                "id": 6,
                "children": [],
                "name": "Child 21"
              }
            ],
            "name": "Parent 2",
            "properties": [
              {
                "id": 21,
                "run": "medium"
              },
              {
                "id": 22,
                "walk": "fast"
              }
            ]
          },
          {
            "id": 7,
            "type": "Parent",
            "children": [
              {
                "id": 8,
                "children": [],
                "name": "Child 31"
              }
            ],
            "name": "Parent 3",
            "properties": [
              {
                "id": 31,
                "run": "fast"
              },
              {
                "id": 32,
                "walk": "slow"
              }
            ]
          }
        ]
      }
      iterateTree(treeStructure)
      iterateTree (node) {
              var self = this;
              function recursive(obj) {
                  if (obj.children && obj.children.length) {
                      obj.children.forEach(function(val,key){
                          if(val.hasOwnProperty("children")){
                            self.modifyProperties(val);
                            recursive(val.children);
                          }
                    }) 
                  }
              }
            var expectedOutput =  recursive(node);
          console.log(expectedOutput)
          }
          modifyProperties(nodeData) {
              let thingProperties = [];
              if (nodeData.properties) {
                  nodeData.properties.map(function (property) {
                      let tempObj = {
                          "id": null,
                          "actionType": "create",
                          "run" : property.run

                      };
                      thingProperties.push(tempObj);
                  })
              }
              return {
                      "id": nodeData.id,
                      "name": nodeData.name,
                      "actionType": "create",

              }
          }

预期的输出:我应该能够将“ id”修改为null并添加           为子元素和父元素创建的“ actionType”,如下所示

      {
        "id": 1,
        "name": "Grand Parent 1",
        "actionType": "create",
        "children": [
          {
            "id": 2,
            "actionType": "create",
            "children": [
              {
                "id": 3,
                "children": [],
                "name": "Child 11",
                "actionType": "create",
                "properties": [
                  {
                    "id": null,
                    "run": "fast",
                    "actionType": "create",
                    "read": "slow"
                  },
                  {
                    "id": null,
                    "actionType": "create",
                    "walk": "slow",
                    "write": "fast"
                  }
                ]
              },
              {
                "id": 4,
                "type": "Child",
                "children": [],
                "name": "Child 12",
                "actionType": "create",
                "properties": [
                  {
                    "id": null,
                    "actionType": "create",
                    "run": "slow"
                  },
                  {
                    "id": null,
                    "actionType": "create",
                    "walk": "medium"
                  }
                ]
              }
            ],
            "name": "Parent 1",
            "actionType": "create",
            "properties": [
              {
                "id": null,
                "actionType": "create",
                "run": "slow"
              },
              {
                "id": null,
                "actionType": "create",
                "walk": "fast"
              }
            ]
          },
          {
            "id": 5,
            "children": [
              {
                "id": null,
                "children": [],
                "name": "Child 21"
              }
            ],
            "name": "Parent 2",
            "actionType": "create",
            "properties": [
              {
                "id": null,
                "actionType": "create",
                "run": "medium"
              },
              {
              "id": null,
                "walk": "fast"
              }
            ]
          },
          {
            "id": 7,
            "type": "Parent",
            "actionType": "create",
            "children": [
              {
                "id": null,
                "actionType": "create",
                "children": [],
                "name": "Child 31"
              }
            ],
            "name": "Parent 3",
            "properties": [
              {
              "id": null,
              "actionType": "create",
                "run": "fast"
              },
              {
                "id": null,
                "actionType": "create",
                "walk": "slow"
              }
            ]
          }
        ]
      }

0 个答案:

没有答案