1

I have a recursive array with same structure of objects and it contains name property. My requirement is to add new property id along with name in recursive array of objects

below is my sample array

[
    {
        "children": [
            {
                "children": [
                    {
                        "children": [
                            {
                                "children": [],
                                "name": "ID01",
                                "type": "Under"
                            },
                            {
                                "children": [],
                                "name": "ID02",
                                "type": "Under"
                            }
                        ],
                        "name": "httpgateway",
                        "type": "Gut"
                    },
                    {
                        "children": [
                            {
                                "children": [],
                                "name": "mock1",
                                "type": "Under"
                            },
                            {
                                "children": [],
                                "name": "mock2",
                                "type": "Under"
                            }
                        ],
                        "name": "mock",
                        "type": "Gut"
                    }
                ],
                "name": "23131",
                "type": "SEV"
            }
        ],
        "name": "integration",
        "type": "DataCenter"
    },
    {
        "children": [
            {
                "children": [
                    {
                        "children": [
                            {
                                "children": [],
                                "name": "data1",
                                "type": "Under"
                            },
                            {
                                "children": [],
                                "name": "data12",
                                "type": "Under"
                            },
                            {
                                "children": [],
                                "name": "data13",
                                "type": "Under"
                            },
                            {
                                "children": [],
                                "name": "data14",
                                "type": "Under"
                            }
                        ],
                        "name": "Gut1",
                        "type": "Gut"
                    }
                ],
                "name": "213213",
                "type": "SEV"
            }
        ],
        "name": "dev",
        "type": "dt"
    }
]

I need Id property along with name as belo

[
    {
        "children": [
            {
                "children": [
                    {
                        "children": [
                            {
                                "children": [],
                                "name": "ID01",
                                "id": "ID01",
                                "type": "Under"
                            },
                            {
                                "children": [],
                                "name": "ID02",
                                "id": "ID02",
                                "type": "Under"
                            }
                        ],
                        "name": "gate",
                        "id": "gate",
                        "type": "Gut"
                    },
                    {
                        "children": [
                            {
                                "children": [],
                                "name": "mock1",
                                "id": "mock1",
                                "type": "Under"
                            },
                            {
                                "children": [],
                                "name": "mock2",
                                "id": "mock2",
                                "type": "Under"
                            }
                        ],
                        "name": "mock",
                        "name": "id",
                        "type": "Gut"
                    }
                ],
                "name": "23131",
                "id": "23131",
                "type": "SEV"
            }
        ],
        "name": "int",
        "id": "int",
        "type": "dt"
    },
    {
        "children": [
            {
                "children": [
                    {
                        "children": [
                            {
                                "children": [],
                                "name": "data1",
                                "id": "data1",
                                "type": "Under"
                            },
                            {
                                "children": [],
                                "name": "data12",
                                "id": "data12",
                                "type": "Under"
                            }
                        ],
                        "name": "Gut1",
                        "id": "Gut1",
                        "type": "Gut"
                    }
                ],
                "name": "213213",
                "id": "213213",
                "type": "SEV"
            }
        ],
        "name": "dev",
        "id": "dev",
        "type": "dt"
    }
]

I have written method to update this but its not working as expected

const getTreeItemsFromData = (treeItems) => {
  console.log('---------------------------', treeItems)
  let finalData = []
  return treeItems.map((treeItemData) => {
    let children = undefined;
    if (treeItemData.children && treeItemData.children.length > 0) {
      children = this.getTreeItemsFromData(treeItemData.children);
    }

    let uniqueId = `${treeItemData.name}${Math.floor(Math.random()*(999-100+1)+100)}`;
    finalData.push(treeItemData)
    console.log("-- ------------------", treeItemData)
    
  });
};
Puneet Bhandari
  • 337
  • 5
  • 14
  • 1
    Like this? https://jsfiddle.net/xhd4gufs/ –  Jul 30 '20 at 08:15
  • *its not working as expected* – you should describe the actual and the expected behavior and your attempts to debug if you want some help with debugging at SO – YakovL Jul 30 '20 at 16:15

2 Answers2

0

You jus need a function to accept the array and check if the key children exists and is array, make changes to it and then recursively call if it has children.

const t = [
  {
    "children": [
      {
        "children": [
          {
            "children": [
              {
                "children": [
                  
                ],
                "name": "data1",
                "type": "Under"
              },
              {
                "children": [
                  
                ],
                "name": "data12",
                "type": "Under"
              },
              {
                "children": [
                  
                ],
                "name": "data13",
                "type": "Under"
              },
              {
                "children": [
                  
                ],
                "name": "data14",
                "type": "Under"
              }
            ],
            "name": "Gut1",
            "type": "Gut"
          }
        ],
        "name": "213213",
        "type": "SEV"
      }
    ],
    "name": "dev",
    "type": "dt"
  }
];
function addIdRec(arr){
    arr.forEach(a => {
        if(a.children instanceof Array){
            a.id = a.name;
            if(a.children.length > 0){
                addIdRec(a.children);
            }
        }
        
    })
}
addIdRec(t)
Anurag
  • 643
  • 1
  • 11
  • 28
0

We can do this with a pretty simple recursion:

const addId = (data) => 
  data .map (({name, children, ...rest}) =>
    ({children: addId(children), name, id: name, ...rest})
  )

const data = [{children: [{children: [{children: [{children: [], name: "ID01", type: "Under"}, {children: [], name: "ID02", type: "Under"}], name: "httpgateway", type: "Gut"}, {children: [{children: [], name: "mock1", type: "Under"}, {children: [], name: "mock2", type: "Under"}], name: "mock", type: "Gut"}], name: "23131", type: "SEV"}], name: "integration", type: "DataCenter"}, {children: [{children: [{children: [{children: [], name: "data1", type: "Under"}, {children: [], name: "data12", type: "Under"}, {children: [], name: "data13", type: "Under"}, {children: [], name: "data14", type: "Under"}], name: "Gut1", type: "Gut"}], name: "213213", type: "SEV"}], name: "dev", type: "dt"}]

console .log (addId (data))
.as-console-wrapper {max-height: 100% !important; top: 0}

We simply clone the node, adding an id property to match the name one and recur on the children property.

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103