js递归汇总
2021-02-12 15:18
标签:对象 als break int -- ret 属性 turn cte js递归汇总 标签:对象 als break int -- ret 属性 turn cte 原文地址:https://blog.51cto.com/10983441/2500348[
{
"pid":1028,
"id":"2c91851f6b6f07b8016bd19ab465000b",
"siteId":"2c9185c36a262618016ad3f8c2a4001c",
"shape":"",
"name":"item1",
"category":"voice",
"point":"10",
"type":1,
"url":"",
"icon":"smile",
"parentId":null,
"isInherit":false,
"createTime":"2019-07-08T12:41:45.000+0000",
"isParent":null,
"childs":[
{
"pid":1028,
"id":"2c91851f6b6f07b8016bqwqw00b",
"siteId":"2c9185c36a262618016ad3f8c2a4001c",
"shape":"",
"name":"item3",
"category":"voice",
"point":"10",
"type":1,
"url":"",
"icon":"smile",
"parentId":null,
"isInherit":false,
"createTime":"2019-07-08T12:41:45.000+0000",
"isParent":null,
"childs":[
{
"pid":1028,
"id":"2c91851f6bsdafaf07ddddw00b",
"siteId":"2c9185c36a262618016ad3f8c2a4001c",
"shape":"",
"name":"item7",
"category":"voice",
"point":"10",
"type":1,
"url":"",
"icon":"smile",
"parentId":null,
"isInherit":false,
"createTime":"2019-07-08T12:41:45.000+0000",
"isParent":null,
"childs":null
}
]
},
{
"pid":1028,
"id":"2c91851f6b6asas6bqwqw00b",
"siteId":"2c9185c36a262618016ad3f8c2a4001c",
"shape":"",
"name":"item4",
"category":"voice",
"point":"10",
"type":1,
"url":"",
"icon":"smile",
"parentId":null,
"isInherit":false,
"createTime":"2019-07-08T12:41:45.000+0000",
"isParent":null,
"childs":null
}
]
},
{
"pid":1029,
"id":"2c91851f6b6f07b8016bd19c9431000c",
"siteId":"2c9185c36a262618016ad3f8c2a4001c",
"shape":"voice",
"name":"item2",
"category":"act",
"point":"15101",
"type":2,
"url":"",
"icon":"crown",
"parentId":"2c91851f6b6f07b8016bd19ab465000b",
"isInherit":false,
"createTime":"2019-07-08T12:43:48.000+0000",
"isParent":null,
"childs":[
{
"pid":1028,
"id":"2c91851f6b6f07ddddw00b",
"siteId":"2c9185c36a262618016ad3f8c2a4001c",
"shape":"",
"name":"item5",
"category":"voice",
"point":"10",
"type":1,
"url":"",
"icon":"smile",
"parentId":null,
"isInherit":false,
"createTime":"2019-07-08T12:41:45.000+0000",
"isParent":null,
"childs":null
},
{
"pid":1028,
"id":"2c91851f6b6asasaaaqwqw00b",
"siteId":"2c9185c36a262618016ad3f8c2a4001c",
"shape":"",
"name":"item6",
"category":"voice",
"point":"10",
"type":1,
"url":"",
"icon":"smile",
"parentId":null,
"isInherit":false,
"createTime":"2019-07-08T12:41:45.000+0000",
"isParent":null,
"childs":null
}
]
}]
1.1根据勾选的最底层 子节点 ,获取权限树 数据
getPriTreeDataBySelectedPriIdList(treeData, priIdList){
//根据最底层子节点,获取权限树数据
return treeData.filter(item=>{
if(item.childs && item.childs.length > 0 ){
item.childs = this.getPriTreeDataBySelectedPriIdList(item.childs,priIdList);
return item.childs && item.childs.length >0;
}else {
return priIdList.indexOf(item.id) > -1;
}
});
},
1.2重置权限树数据
resetTreeData(priTreeData){
//重置权限树数据
priTreeData.map(item => {
item.isInherit = false;
if(item.childs && item.childs.length>0){
this.resetTreeData(item.childs);
}
})
},
1.3根据任意ID获取item对象
getItemFromPriTreeData(priTreeData, priId){
//根据任意ID获取item对象
let result = null;
for(let priItemIndex in priTreeData){
if(result !== null){
break;//这里是关键
}
let priItem = priTreeData[priItemIndex];
if(priItem.id === priId){
result = priItem;
break;
}else if(priItem.childs && priItem.childs.length > 0){
result = this.getItemFromPriTreeData(priItem.childs, priId);
}
}
return result;
},
1.4父节点勾选了,勾选所有的子节点
setIsInheritValueWithParentPri(priList, isInherit){
// 递归的设置"是否继承属性"
// 父节点勾选是否继承,子节点跟随父节点变化 子节点变化,父节点不跟随变化
priList.map(priItem => {
if(priItem.type !== 3){
priItem.isInherit = isInherit;
}
if(priItem.childs && priItem.childs.length > 0){
this.setIsInheritValueWithParentPri(priItem.childs, isInherit);
}
})
},
1.5设置详情页中---展示树数据中的isInherit
setInheritOfShowPriTreeData(showPriTreeData, priTreeDataPriIdAndHeritObject){
// 设置详情页中---展示树数据中的isInherit
showPriTreeData.map(priItem => {
priItem.isInherit = priTreeDataPriIdAndHeritObject[priItem.id];
if(priItem.childs && priItem.childs.length > 0){
this.setInheritOfShowPriTreeData(priItem.childs, priTreeDataPriIdAndHeritObject )
}
})
},
1.6设置选中的权限树数据相关信息到priTreeDataPriIdAndHeritObject,priIdListIncludeParentPriId中
setPriTreeDataPriIdAndHeritObject(priTreeDataBySelectedPriIdList, priTreeDataPriIdAndHeritObject,priIdListIncludeParentPriId){
// 设置选中的权限树数据相关信息到priTreeDataPriIdAndHeritObject,priIdListIncludeParentPriId中
//priTreeDataPriIdAndHeritObject是对象,key是权限ID,value是isInherit
//priIdListIncludeParentPriId 根据最底层子节点获得的权限树数据,把所有的权限ID都放到列表中
priTreeDataBySelectedPriIdList.map(priItem => {
priIdListIncludeParentPriId.push( priItem.id);
if(priItem.isInherit){
priTreeDataPriIdAndHeritObject[ipriItem.id] = priItem.isInherit;
}else{
priTreeDataPriIdAndHeritObject[priItem.id] = false;
}
if(priItem.childs && priItem.childs.length>0){
this.setPriTreeDataPriIdAndHeritObject(priItem.childs, priTreeDataPriIdAndHeritObject,priIdListIncludeParentPriId)
}
})
}