• 递归简化数据处理方式


    递归简化数据 处理前的数据
    [
    {
    “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
  • 相关阅读:
    河南双创蓝皮书发布:科技创新持续发力,​中创助推中部地区发展!
    Apache Ignite 使用SQL模式
    数据中台为什么不好搞?
    三菱PLC网络MC3E通信—读取或写入——字符串或数字
    Java中三种方法重复使用同一输入流
    Abbexa丨Abbexa低样本量通用皮质醇ELISA试剂盒原理
    说说未来趋势 「元宇宙」是什么?
    Hadoop3 - MapReduce 属性优化
    牛客《算法入门》链表(题解C++)
    关于道一云-七巧使用感悟
  • 原文地址:https://blog.csdn.net/weixin_42930684/article/details/126847486