【发布时间】:2017-07-02 19:34:31
【问题描述】:
我有一个这样的 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")
有没有办法做到这一点? 我不知道如何解决这个问题,因为我需要密钥的路径才能编辑树。
【问题讨论】:
-
@Andreas 感谢您指出这一点
标签: javascript json