zoukankan      html  css  js  c++  java
  • json list数据递归生成树状层级JSON

    <!DOCTYPE html>
    
    <html>
    <head>
    <script>
    var data=[
    {"id":"aaa","parentId":"account","spType":0,"layerId":0,"seqId":1,"name":"阿萨德发多少","deleted":"0"},
    {"id":"account","parentId":"","spType":0,"layerId":0,"seqId":50,"name":"账户","deleted":"0"},
    {"id":"bbb","parentId":"account","spType":0,"layerId":0,"seqId":2,"name":"阿萨德发多少","deleted":"0"},
    {"id":"ccc","parentId":"account","spType":0,"layerId":0,"seqId":3,"name":"a啊都是发","deleted":"0"},
    {"id":"ddd","parentId":"dispatch","spType":0,"layerId":0,"seqId":1,"name":"大夫","deleted":"0"},
    {"id":"dispatch","parentId":"","spType":0,"layerId":0,"seqId":2,"name":"通知公告","deleted":"0"},
    {"id":"eee","parentId":"dispatch","spType":0,"layerId":0,"seqId":2,"name":";卡萨丁","deleted":"0"},
    {"id":"fff","parentId":"gridding","spType":0,"layerId":0,"seqId":1,"name":"拉收到了","deleted":"0"},
    {"id":"gridding","parentId":"","spType":0,"layerId":0,"seqId":1,"name":"网格化管理","deleted":"0"},
    {"id":"portals","parentId":"","spType":0,"layerId":0,"seqId":3,"name":"综合信息门户管理","deleted":"0"}
    ];
     
       var getJsonTree=function(data,parentId){
            var itemArr=[];
            for(var i=0;i<data.length;i++){ 
                var node=data[i];
                //data.splice(i, 1)
                 if(node.parentId==parentId ){ 
                    var newNode={id:node.id,title:node.name,nodes:getJsonTree(data,node.id)};
                    itemArr.push(newNode);              
                 }
            }
            return itemArr;
        }
         
        console.log(getJsonTree(data,''));
         
         
    </script>
    </head>
     
    <body>
     
     
    </body>
    </html>

    转:https://www.cnblogs.com/pangguoming/p/7373991.html

  • 相关阅读:
    topcoder srm 633 div1
    HDU 4997 Biconnected (状态压缩DP)
    HDU 5013 City Tour
    BZOJ 3672 [Noi2014]购票 (熟练剖分+凸壳维护)
    BZOJ 1488: [HNOI2009]图的同构 polay
    BZOJ 1565 植物大战僵尸(最大权闭合图)
    iOS加载程序视图的方式
    内存管理2(主讲MRR)
    内存管理1
    排序算法之希尔排序
  • 原文地址:https://www.cnblogs.com/horanly/p/8052515.html
Copyright © 2011-2022 走看看