我JSON像这样的数组:JSON:编辑嵌套很深的值
var tree = [
{
text: "Parent 1",
id: 1,
nodes: [
{
text: "Child 1",
id: 2,
nodes: [
{
text: "Grandchild 1"
id: 3,
},
{
text: "Grandchild 2"
id: 4,
nodes: [
{
text: "Grandchild 3"
id: 10,
},
{
text: "Grandchild 4"
id: 11,
nodes: [
{
text: "Grandchild 5"
id: 12,
},
{
text: "Grandchild 6"
id: 13,
}
]
}
]
}
]
},
{
text: "Child 2"
id: 5,
}
]
},
{
text: "Parent 2"
id: 6,
},
{
text: "Parent 3"
id: 7,
},
{
text: "Parent 4"
id: 8,
},
{
text: "Parent 5"
id: 9,
}
];
我试图创造条件,采取为参数树,ID和newText参数的函数,即将找到具有给定id的节点,用newText替换文本,并返回修改后的json。
例:
editTree(tree, 11, "Granchild 13435")
有没有办法来实现这一目标? 我不知道如何解决这个问题,因为我需要密钥的路径才能编辑树。
[没有这样的事情作为“JSON对象”](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – Andreas
@Andreas谢谢你为指出这 –
试试这个答案:http://stackoverflow.com/questions/22222599/javascript-recursive-search-in-json-object – jonasfh