data = [
{
value: 1,
name:"1",
children:[
{
value: 2,
name:"2",
}
]
},
{
value: 3,
name:"3",
children:[
{
value: 4,
name:"4",
}
]
}];
val = 4;
// 反向查找父节点
getNodePath(node, val, path) { // node:所有数据,val:后盾返回的id, path:要渲染的数组id
for (let i = 0; i < node.length; i++) {
const ele = node[i];
if (ele.value == val) {
path.push(ele.value);
return path
} else if (ele.children && ele.children.length > 0) {
if (ele.children.some(row => row.value == val)) {
path.unshift(ele.value);
this.getNodePath(ele.children,val,path);
} else {
this.getNodePath(ele.children,val,path);
}
}
}
return path;
},
//应用
puth = this.getNodePath(data,val,[]);
// [3,4]
发表回复