• 递归简化数据处理方式


    递归简化数据 处理前的数据
    [
    {
    “guid”: “C81242A4E81A278BE0501AACD4C71F2D”,
    “code”: “01”,
    “name”: “预算科”,
    “children”: [
    {
    “guid”: “C81397AB80A17B29E0501AACD4C7622A”,
    “code”: “907”,
    “name”: “包头市财政局(预算)”,
    “children”: [
    {
    “guid”: “C81397AB80A27B29E0501AACD4C7622A”,
    “children”: [],
    “code”: “907001”,
    “name”: “预算科本级”,
    }
    ]
    }
    ],
    }
    ]
    处理方式

     getMiniNodes(array) {
        const filterList = []
        array.forEach((ele) => {
          const { children, guid } = ele
          if (children && children.length) {
            const currentEle = {
              children: [],
              guid
            }
            const result = this.getMiniNodes(children)
            if (result.length) {
              currentEle.children = result
              filterList.push(currentEle)
            }
          } else {
            filterList.push({ guid })
          }
        })
        return filterList
      }
      }```
    处理后的数据
    [
        {
            "guid": "C81242A4E81A278BE0501AACD4C71F2D",
            "children": [
                {
                    "guid": "C81397AB80A17B29E0501AACD4C7622A",
                    "children": [
                        {
                            "guid": "C81397AB80A27B29E0501AACD4C7622A",
                        }
                    ]
                }
            ]
        }
    ]
    
    
    递归数据过滤(例如过滤最底层掉符合某个条件的数据)
    ```html
    const filterMember = (key, data) => {
        const filterList = [];
        data.forEach((ele) => {
            const { member, ...other } = ele;
            if (member && member.length) {
                const currentEle = {
                    member: [],
                    ...other
                };
                const result = filterMember(key, member);
    
                if (result.length) {
                    currentEle.member = result;
                    filterList.push(currentEle);
                }
            } else if (other.age > key) {
                filterList.push(other);
            }
        });
        return filterList;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    处理后的数据(过滤掉了最底层的一个数据)

    [
        {
            "guid": "C81242A4E81A278BE0501AACD4C71F2D",
            "children": [
                {
                    "guid": "C81397AB80A17B29E0501AACD4C7622A",
                    "children": [], //过滤掉的数据
                    "code": "907",
                    "name": "包头市财政局(预算)",
                }
            ],
            "code": "01",
            "name": "预算科",
        }
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • 相关阅读:
    【算法与数据结构】216、LeetCode组合总和 III
    前端培训丁鹿学堂:vue性能优化总结(二)
    独立成分分析matlab代码
    谷粒商城--整合SpringCloud Alibaba
    Java项目自动生成接口文档
    shell命令梳理
    K8S LoadBalancer kube-vip 部署
    新手小白适合做哪个跨境电商平台?测评自养号能带来哪些收益及优势?
    事务的特性
    vue3+ts封装弹窗,分页封装
  • 原文地址:https://blog.csdn.net/weixin_42930684/article/details/126847486