树形结构,通过子节点,获取祖先节点
2022-11-03 本文已影响0人
芸芸众生ing
function getValuePath(list , val ) {
const { value, children } = this.PROPS;
const arr = [];
getObject(list, 0);
function getObject(listObj , idx ) {
let bool = false;
for (let item of listObj) {
if (item[value] === val) {
bool = true;
arr[idx] = item;
break;
} else if (item[children]?.length) {
bool = getObject(item[children], idx + 1);
if (bool) {
arr[idx] = item;
break;
}
}
}
return bool;
}
return arr;
},