How to add an object to a javascript nested object using parent id

In my application, I create a JavaScript object based on a JSON response from a server like this:

{ name: "root", id: 1, children: [ { name: "child one", id: 11, children: [ {name: "grand child 1", id: 111, children: []}, {name: "grand child 2", id: 112, children: []} ] }, { name: "child two", id: 12, children: [] } ] } 

I create a new node, for example:

  {name: "grandchild three", id: 113, children:[]} 

With that in mind, how can I add a new grandson to its parent element with id 11? Please note that I do not know the static path to the node with id == 11 , so I wonder how I could get this node just knowing its id .

Edit: note that the identifier in the real case DOES NOT encode the path to the objects. I created this simple example to demonstrate the data structure I'm dealing with. But I can not get the path to the object using its id in my real application.

+4
source share
4 answers

See this script: http://jsfiddle.net/2Dvws/

It will find the object by identifier. And click on the new baby. Since every object in Javascript is a link, you can return it as var.

 var ob = { name: "root", id: 1, children: [ { name: "child one", id: 11, children: [ { name: "grand child 1", id: 111, children: []}, { name: "grand child 2", id: 112, children: []} ]}, { name: "child two", id: 12, children: []} ] }; 

A function that will return the found item. Look at all the children.

 function findObjectById(root, id) { if (root.children) { for (var k in root.children) { if (root.children[k].id == id) { return root.children[k]; } else if (root.children.length) { return findObjectById(root.children[k], id); } } } }; var bla = findObjectById(ob, 111); console.log(bla); bla.children.push({ name: "child x", id: 1111, children: [] }); console.log(ob); 

The conclusion is that the child with id 111 will have 1 child with identifier 1111

+6
source

I assume id consists of parent id plus index (1 to 9) in children array? Then you can go like this:

 var rootobj = {…}; var newnode = {name: "grandchild three", id: 113, children:[]}; var id = ""+newnode.id; var cur = [rootobj]; for (var i=0; i<id.length-i; i++) cur = cur[id.charAt(i)-1].children; cur[id.charAt(i)-1] = newnode; 
+2
source

Niels answer is a good start, but does not completely cross the tree (for example, it will break if the node you are looking for is a descendant of the second child of the root). It also breaks if the root is the identifier you are looking for. Here are my clarifications:

  function findObjectByID(root, id) { if (root.name == id){ return root; } if (root.children) { for (var k in root.children) { if (root.children[k].name == id) { return root.children[k]; } else if (root.children[k].children) { result = findObjectByID(root.children[k], id); if (result) { return result; } } } } }; 
+1
source

How about this one.

 for(var a = 0; a < object.length; a++) { for(var b = 0; b < obj.children.length; b++) { if(object[a].children[b].id == 11) { object[a].children[b].children.push({ name: "grandchild three", id: 113, children: [] }); } } } 
0
source

Source: https://habr.com/ru/post/1439854/


All Articles