zoukankan      html  css  js  c++  java
  • 线性数据转为树形数据

    let list = [ 
        { pid: 0, id: 1, value: '总公司' },
        { pid: 3, id: 2, value: '人事部-1' }, 
        { pid: 1, id: 3, value: '人事部' },
        { pid: 1, id: 4, value: '采购部' }, 
        { pid: 1, id: 5, value: '组织部' }, 
    ]; 
    
    function listToTree(list){
        //遍历整个列表
        return list.filter(cur=>{ 
            // 获取当前节点的子节点
            let children= list.filter(item=> item.pid== cur.id ); 
            if(children.length>0){
                 cur.children=children;
            }
            //只返回顶级节点
            return cur.pid==0; 
        });
    }
    
    console.log(listToTree(list));
  • 相关阅读:
    shell 参数个数
    小坑也难受
    MaHua简介
    airflow Operators
    datax
    T-SQL 更新表操作
    T-SQL时间函数
    linux学习网站
    好看的页面
    函数-1
  • 原文地址:https://www.cnblogs.com/Rhea-rui/p/13554207.html
Copyright © 2011-2022 走看看