js list数据 转 树状 层级 JSON,递归生成树状 层级 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>

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏鬼谷君

python 练习多级菜单思路

671
来自专栏草根专栏

用VSCode开发一个asp.net core2.0+angular5项目(5): Angular5+asp.net core 2.0 web api文件上传

这部分就讲从angular5的客户端上传图片到asp.net core 2.0的 web api. 这是需要的源码: https://pan.baidu.com...

3465
来自专栏张戈的专栏

网站集成打字震动特效JS代码改进版

这又是一个拖欠了很久的分享,很早就有朋友留言问评论打字炫彩、震动特效怎么实现的。这功能其实网上早就有人分享 N 遍了,有点搜索技巧和 DIY 能力的站长同学也早...

3237
来自专栏青玉伏案

iOS开发之微信聊天工具栏的封装

之前山寨了一个新浪微博(iOS开发之山寨版新浪微博小结),这几天就山寨个微信吧。之前已经把微信的视图结构简单的拖了一下(iOS开发之微信山寨版),今天就开始给微...

1968
来自专栏Alice

绘图quartz之加水印

实现在图片上加一个水印  并存在document的路径下  同时在手机相册中也存一份 //首先开启imageContext找到图片     UIGraphics...

1856
来自专栏函数式编程语言及工具

Akka(29): Http:Server-Side-Api,Low-Level-Api

 Akka-http针对Connection的两头都提供了方便编程的Api,分别是Server-Side-Api和Client-Side-Api。通过这两个Ap...

1798
来自专栏Android机动车

AndroidStudio3.0多渠道打包:我用一行命令打出N个包

csdn: http://blog.csdn.net/k_bb_666

351
来自专栏Netkiller

Hyperledger Fabric 积分代币上链方案

中国广东省深圳市龙华新区民治街道溪山美地 518131 +86 13113668890 <netkiller@msn.com>

3K13
来自专栏DannyHoo的专栏

iOS开发中利用runtime设置UITextView的默认文字

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010105969/article/details/...

402
来自专栏Guangdong Qi

小Q项目框架搭建及会动的Tabbar未完待续,持续更新中

1424

扫描关注云+社区