// 根据id获取父节点组成idList
const getParentId =(list, id, children)=> {
for (let i in list) {
if (list[i].id == id) {
return [list[i].id];
}
if (list[i][children]) {
let node = getParentId(list[i][children], id, children);
if (node !== undefined) {
return node.concat(list[i].id);
}
}
}
}
// /递归根据某个属性删除节点
const removeNodeByValue =(tree, children, value, valueParams)=> {
let forFn = (list) => {
list.forEach((node, index) => {
if (valueParams === node[value]) {
list.splice(index, 1);
}
if (node[children]) forFn(node[children]);
});
};
forFn(tree);
}
// 递归根据某个属性获取节点的某个属性 value2List
const getTreeValue2ListByValue= (tree, children, value, valueParams,value2)=> {
let value2List = []
let forFn = (list) => {
list.forEach((node, index) => {
if (valueParams === node[value]) {
value2List.push(node[value2]);
}
if (node[children]) forFn(node[children]);
});
};
forFn(tree);
return value2List
}
//递归获取最后一层的valueList
const getLastChildValueList =(tree, children, value)=> {
let valueList = []
let forFn = (list) => {
list.forEach((node, index) => {
if (node[children]) forFn(node[children]);
else valueList.push(node[value])
});
};
forFn(tree);
return valueList
}
工作中可能用到的一些递归算法
最新推荐文章于 2025-08-08 15:25:35 发布