我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