const findParentNode = (arr, key, parent)=>{
for (let i = 0; i < arr.length; i++) {
if (arr[i].key === key) {
// 找到正确的结果代表最后一层的返回值
return parent
}
if (arr[i].children) {
const target = findParentNode(arr[i].children, key, arr[i])
if (target) {
// 返回值给到上一层,以此类推,直到最顶层
return target
}
}
}
return null
}
const arr=[{
title: 'hyz', key: '0',
children:[{
title: 'hyzson',
key: '0-1',
children:[{
title:'hyz’so’son',
key:'0-1-0'
}]
}]
}]
let tarGet = findParentNode(arr,'0-1-0')
console.log(tarGet)