- 普通递归算法
xxzxServ.getSelectedNodes = function(treeList, m) {
for(let i=0;i<treeList.length;i++){
if (treeList[i].selected === true) {
m.push(treeList[i])
}
let children = treeList[i].children;
if (Array.isArray(children) && children.length > 0) {
children.forEach(function (item) {
xxzxServ.getCheckedNodes([item], m)
})
}
}
return m
};
- 进阶递归:对组织树进行递归,若节点为图层节点,那么此节点被删掉不显示。同时增加判断,若父节点的子节点全被删掉了,那么要被父节点置空。
function treeFormat(treeList) {
for(let i=0;i<treeList.length;i++){
if (treeList[i].layerId.length > 0) {
delete treeList[i];
continue
}
if(treeList[i].hasOwnProperty('children')){
let children = treeList[i].children;
if (Array.isArray(children) && children.length > 0) {
treeFormat(children)
}
}
//这一步是为了 删除数组项后,被删除项会被定义为empty,且数组长度不变
//此时需要通过filter来过滤数组,实现数组长度被降低
if(Array.isArray(treeList[i].children)){
treeList[i].children =
treeList[i].children.filter(item=>item !== undefined);
}
}
}