【发布时间】:2018-07-17 11:47:11
【问题描述】:
我有这样的数据:
{
{
"text" : "parent1",
"nodes" :[
{
"text": "child1",
"nodes": [
{
"text": "grandchild1",
"nodes":[
{
"text": "hello",
"nodes": []
}
]
},
{
"text": "hello",
"nodes":[]
}
]
}
]
},
{
"text" : "parent2",
"nodes" :[
{
"text": "child2",
"nodes": [
{
"text": "grandchild2",
"nodes": [
{
"text": "grandgrandchild1",
"nodes": [
{
"text": "hello",
"nodes": []
}
]
}
]
}
]
}
]
}
}
我想要的是创建一个路径数组,其中包含“文本”值为“hello”的元素的路径。比如根据这个数据:
var paths: any[][] = [
["parent1","child1","grandchild1","hello"],
["parent1","child1","hello"],
["parent2","child2","grandchild2","grandgrandchild1","hello"]
];
我想要这个“路径”数组。请注意,如果元素的文本值为“hello”,那么该元素的“nodes”长度为0。我想我在递归中犯了一个错误。
【问题讨论】:
-
我认为我在递归中犯了一个错误请分享你的尝试。
标签: javascript arrays json recursion nested