In my application I create a JavaScript object based on a JSON response from the server similar to this:
在我的应用程序中,我基于来自服务器的JSON响应创建一个JavaScript对象,类似于:
{
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 such as:
我创建了一个新节点,例如:
{name: "grandchild three", id: 113, children:[]}
With this in mind, how can I add this new grandchild to its parent with id 11? Please note that I don’t know the static path to node with id == 11
so I am wondering how I could obtain that node with just knowing it's id
.
考虑到这一点,我如何将这个新孙子添加到ID为11的父母?请注意,我不知道id为= 11的节点的静态路径,所以我想知道如何通过知道它的id来获取该节点。
Edit: please note the id's in the real case do NOT encode the path to objects. I created this simple example for demonstration of the data structure I am dealing with. But I can not retrieve the path to the object using its id in my real application.
编辑:请注意真实案例中的id不编码对象的路径。我创建了这个简单的例子来演示我正在处理的数据结构。但我无法在我的实际应用程序中使用其ID检索对象的路径。
4 个解决方案
#1
6
See this fiddle: http://jsfiddle.net/2Dvws/
看到这个小提琴:http://jsfiddle.net/2Dvws/
It will find an object by ID. And push the new child. Since every object within Javascript is a reference, you can return it as an var.
它会通过ID找到一个对象。并推动新的孩子。由于Javascript中的每个对象都是引用,因此可以将其作为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: []}
]
};
The function which will return the found element. Will look into all child elements.
将返回找到的元素的函数。将调查所有子元素。
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);
Output is that child with id 111 will have 1 child with id 1111
输出是id为111的孩子将有1个id为1111的孩子
#2
2
I assume the id consists of the parent-id plus the index (1 to 9) in the children array? Then you can go like that:
我假设id包含parent-id加上children数组中的索引(1到9)?然后你可以这样:
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;
#3
1
Niels' answer is a good start, but doesn't fully traverse the tree (e.g. it will break if the node you're looking for is the child of the second child of the root). Also it breaks if the root is the id you're looking for. Here are my refinements:
Niels的回答是一个好的开始,但没有完全遍历树(例如,如果您正在寻找的节点是根的第二个孩子的孩子,它将会中断)。如果root是你正在寻找的id,它也会中断。以下是我的改进:
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;
}
}
}
}
};
#4
0
How about this.
这个怎么样。
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: []
});
}
}
}
#1
6
See this fiddle: http://jsfiddle.net/2Dvws/
看到这个小提琴:http://jsfiddle.net/2Dvws/
It will find an object by ID. And push the new child. Since every object within Javascript is a reference, you can return it as an var.
它会通过ID找到一个对象。并推动新的孩子。由于Javascript中的每个对象都是引用,因此可以将其作为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: []}
]
};
The function which will return the found element. Will look into all child elements.
将返回找到的元素的函数。将调查所有子元素。
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);
Output is that child with id 111 will have 1 child with id 1111
输出是id为111的孩子将有1个id为1111的孩子
#2
2
I assume the id consists of the parent-id plus the index (1 to 9) in the children array? Then you can go like that:
我假设id包含parent-id加上children数组中的索引(1到9)?然后你可以这样:
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;
#3
1
Niels' answer is a good start, but doesn't fully traverse the tree (e.g. it will break if the node you're looking for is the child of the second child of the root). Also it breaks if the root is the id you're looking for. Here are my refinements:
Niels的回答是一个好的开始,但没有完全遍历树(例如,如果您正在寻找的节点是根的第二个孩子的孩子,它将会中断)。如果root是你正在寻找的id,它也会中断。以下是我的改进:
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;
}
}
}
}
};
#4
0
How about this.
这个怎么样。
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: []
});
}
}
}