0

I have a scenario were need to iterate over each children element of the tree structure and modify/add properties or attributes. Each children can have multiple children

      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",

              }
          }

Expected Output: I should be able to modify "id" as null and add "actionType" as create for children and parent element as shown below

      {
        "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"
              }
            ]
          }
        ]
      }
  • Have a look [here](https://stackoverflow.com/questions/722668/traverse-all-the-nodes-of-a-json-object-tree-with-javascript) to see how you iterate over JSON. The modifiying can then be done on each level using `obj.property = "myValue"`. – Björn Böing Apr 24 '19 at 13:35
  • @BreakBB - I'm facing one problem. Consider as per my example both "Parent 1" and "Parent 2" has same children property "Child 11". If i modify the "Child 11" property specify to "Parent 2" the other child property of "Parent 1" also gets changed. Is it possible to keep the modification specific to only one parent? – Harish Ashok Megharaj Apr 29 '19 at 10:57
  • If "Parent 1" and "Partent 2" have **the same** "Child 11", editing this child should change it for both parents from the logical perspective. If the parents have different children with only the same name/id you should edit them in connection to their parents. Maybe you should shrink your code example to a minimum to make your question more clear. If you now have a different question open up a new one and answer/delete this one. – Björn Böing Apr 29 '19 at 11:16

0 Answers0