• 数据处理


    1、问题描述:

      将当前数组中的(`name: '/'`)删除掉,并将当前children中的数组合并到父数组去。
    

    2、数据格式:

    
    var data = [{
      name: "A",
      children: [{
        name: "1",
        children: [{
            name: "01",
            children: [{
                name: "001",
                children: []
            }]
        }]
      }, {
        name: "2",
        pid: 1,
        children: [{
            name: "/",
            pid: 2,
            children: [{
                name: "002",
                pid: 3,
                children: [{
                    name: "0002",
                    pid: 4,
                    children: [{
                        name: "/",
                        pid: 5,
                        children: [{
                                name: "000002",
                                pid: 6,
                                children: []
                            }
      
                        ]
                    }]
      
                }]
            }]
        }]
    }, {
        name: "/",
        pid: 1,
        children: [{
            name: "/",
            pid: 2,
            children: [{
                name: "/",
                pid: 3,
                children: [{
                    name: "0003",
                    pid: 4,
                    children: []
                },{
                      name: "0003",
                      pid: 4,
                      children: []
                }]
            }]
        }]
      }]
    }]
    
    

    3、处理:通过递归方式,深度查找

    function filter(old) {
      let arr = [];
      old && old.forEach((item, index)=> {
        if(item.name === '/') {
          arr.push(...filter(item.children))
        } else {
          item.children = filter(item.children)
          arr.push(item);
        }
    
      })
      return arr;
    }
    console.log(filter(data))
    

    4、结果:

  • 相关阅读:
    设置debian的静态IP
    《深入理解Java虚拟机》学习笔记之最后总结
    《深入理解Java虚拟机》学习笔记之字节码执行引擎
    《深入理解Java虚拟机》学习笔记之类加载
    《深入理解Java虚拟机》学习笔记之工具
    《深入理解Java虚拟机》学习笔记之内存回收
    Proxy源码解析
    Mysql索引
    搞定Hotspot-api
    JNI链接
  • 原文地址:https://www.cnblogs.com/wangyong1997/p/13913247.html
Copyright © 2020-2023  润新知