结果
var data = [
{
"key": "sub1",
"children": [
{
"key": "sub1-1"
}
]
},
{
"key": "sub2",
"children": [
{
"key": "sub2-1",
"children": [
{
"key": "sub2-1-1"
},
{
"key": "sub2-1-2"
},
{
"key": "sub2-1-3",
"children": [
{
"key": "sub2-1-3-1"
}
]
}
]
},
{
"key": "sub2-2"
},
{
"key": "sub2-3"
}
]
},
{
"key": "sub3",
"children": [
{
"key": "sub3-1"
},
{
"key": "sub3-2"
}
]
}
]
const getopenkey = (arr, itme, reulit = []) => {
arr.forEach((v) => {
if (itme.indexOf(v.key) !== -1) {
reulit.push(v.key);
}
if (v.children && v.children.length) {
getopenkey(v.children, itme, reulit)
}
})
return reulit
}
let element = 'sub2-1-3-1'
console.log(getopenkey(data, element))
标签:reulit,遍历,sub2,sub3,父级,getopenkey,key,children From: https://www.cnblogs.com/zxh-bug/p/16599592.html