码迷,mamicode.com
首页 > Web开发 > 详细

json list数据递归生成树状层级JSON

时间:2017-12-17 18:21:36      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:delete   col   patch   pos   seq   isp   splice   count   nbsp   

<!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

json list数据递归生成树状层级JSON

标签:delete   col   patch   pos   seq   isp   splice   count   nbsp   

原文地址:http://www.cnblogs.com/horanly/p/8052515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!