【发布时间】:2021-02-10 03:10:20
【问题描述】:
我有像下面这样的动态数据树结构,但数据是可变的(例如可以将子元素添加到 id:6)元素,我想访问它我知道 id 名称节点例如 id 是 7 我这样访问它
tree.children[0].children[0].children[1]
但是如果我只知道 id 名称,如果我不知道它在哪个节点中,我怎么能从给定的树结构中找到它 为了更好地理解例如我只知道 id 是 7 我想 tree.children[0].children[0].children[1] 这个路径我怎么能动态地做
let tree = {
id: 1,
name: "tree1",
children: [
{
id: 2,
name: "tree2",
children: [
{
id: 4,
name: "tree4",
children: [
{
id: 6,
name: "tree6"
},
{
id: 7,
name: "tree7"
}
]
},
{
id: 5,
name: "tree5"
}
]
},
{
id: 3,
name: "tree3"
}
]
};
【问题讨论】:
-
这次请不要删除您的问题。我正在研究解决方案,但无法提交,因为您删除了之前的问题:)
-
这能回答你的问题吗? Find by key deep in a nested array
标签: javascript reactjs algorithm sorting tree