/**
*
* @param arr 树数据
* @param id 要查找的id值
* @param findProps 查找的属性id名字
* @param childProps 查找的子数据名字
* @returns 返回所有父节点ID的数组,包含本节点id
*/
const findParents = (arr, id, findProps = 'typeId', childProps = 'childrenNode') => {
const stock: any[] = [];
let going = true;
function recursion(arr, id, findProps, childProps) {
arr.forEach((item) => {
if (!going) return;
stock.push(item[findProps]);
if (item[findProps] === id) {
going = false;
} else if (item[childProps]) {
recursion(item[childProps], id, findProps, childProps);
} else {
stock.pop();
}
});
if (going) stock.pop();
}
recursion(arr, id, findProps, childProps);
return stock;
}
let ids = findParents(respond, curDetail?.partner);
console.log(ids) //[1.1-1,1-1-1]
JS树形结构根据子节id点获所有父节点id
于 2023-11-09 09:28:42 首次发布