js递归汇总

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js递归汇总相关的知识,希望对你有一定的参考价值。

1.数据结构介绍

[
    {
        "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)
          }
        })
      }

以上是关于js递归汇总的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode之深度遍历递归与回溯法汇总

c语言代码编程题汇总:输入一个整数n,输出斐波纳猰数列,采用的是递归算法——源代码

Jquery插件 easyUI属性汇总

JavaScript - 代码片段,Snippets,Gist

汇总|打遍天下二叉树

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js