0
we have a JSON array where we have been able to add a child to "East" but we need to add a child to "Ho" under "Air" .

JSON array is like this



[{  
          "name":"External Customer",
          "id":4,
          "parentId":0,
          "is_open":true,
          "children":[  

             {  
                "name":"East",
                "id":20,
                "parentId":4,
                "is_open":true,
                "children":[  
                   {  
                      "name":"O",
                      "id":21,
                      "parentId":20,
                      "is_open":true,
                      "children":[  
                         {  
                            "name":"Ho",
                            "id":22,
                            "parentId":21,
                            "is_open":true,
                            "children":[  
                               {  
                                  "name":"Air",
                                  "id":23,
                                  "parentId":22,
                                  "is_open":true
                               }
                            ]
                         }
                      ]
                      {  
                      "name":"grandchild three",
                      "id":113,
                      "children":[]
      ]}

we have tried adding "Grandchild three" through the below code

for(var a = 0; a < data.length; a++) {
            for(var b = 0; b < data[a].children.length; b++) {
              console.log(data[a].children[b]);
                if(data[a].children[b].id == 18) {
                   data[a].children[b].children.push({
                        name: "grandchild three", 
                        id: 115, 
                        children: []
                    });

                    slSchema.tree=JSON.stringify(data);
                    slSchema.save(function (err) {
                         done(err, slSchema);
                       });
                }
            }
        }

All we need to do is now add a new child to the last child node i.e "Ho".we have been successful in adding a child to the node "East".How to achieve it using node.js ? Thanks in advance for help.

1 Answer 1

1

You can use a recursive approach to find the correct node, like so:

const data = [{  
    "name":"External Customer",
    "id":4,
    "parentId":0,
    "is_open":true,
    "children":[  

    {  
        "name":"East",
        "id":20,
        "parentId":4,
        "is_open":true,
        "children":[  
            {  
                "name":"O",
                "id":21,
                "parentId":20,
                "is_open":true,
                "children":[  
                {  
                    "name":"Ho",
                    "id":22,
                    "parentId":21,
                    "is_open":true,
                    "children":[  
                        {  
                            "name":"Air",
                            "id":23,
                            "parentId":22,
                            "is_open":true
                        }
                    ]
                }
                ]
            }
        ]
    }]
}];


function addChildToNode(data, nodeId, child) {
    if (!Array.isArray(data)) {
        return;
    }

    for(element of data) {
        if (element.id === nodeId && element.children) {
            element.children.push(child);
        } else {
            addChildToNode(element.children, nodeId, child);
        }
    }
}

// The node to add the child to..
let searchNodeId = 22;
let newChild = { name: "New Child", parentId: searchNodeId, id: 10 };

addChildToNode(data, searchNodeId, newChild);

console.log("Result: ", data);

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, your answer helped solve our problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.