Итерация / Обход каждого дочернего элемента для добавления / изменения его свойств - PullRequest
0 голосов
/ 24 апреля 2019

У меня есть сценарий, в котором необходимо перебирать каждый дочерний элемент древовидной структуры и изменять / добавлять свойства или атрибуты. Каждый ребенок может иметь несколько детей

      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» как ноль и добавить "actionType" as create для дочерних и родительских элементов, как показано ниже

      {
        "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"
              }
            ]
          }
        ]
      }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...