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 条评论
登录 后参与评论

相关文章

来自专栏芋道源码1024

熔断器 Hystrix 源码解析 —— 断路器 HystrixCircuitBreaker

本文主要基于 Hystrix 1.5.X 版本 1. 概述 2. HystrixCircuitBreaker 3. HystrixCircuitBreaker....

5327
来自专栏一个爱瞎折腾的程序猿

sqlserver使用存储过程跟踪SQL

USE [master] GO /****** Object: StoredProcedure [dbo].[sp_perfworkload_trace_s...

2070
来自专栏我和未来有约会

Kit 3D 更新

Kit3D is a 3D graphics engine written for Microsoft Silverlight. Kit3D was inita...

2546
来自专栏大内老A

The .NET of Tomorrow

Ed Charbeneau(http://developer.telerik.com/featured/the-net-of-tomorrow/) Exciti...

31810
来自专栏魂祭心

原 canvas绘制clock

4104
来自专栏飞扬的花生

jsencrypt参数前端加密c#解密

      写程序时一般是通过form表单或者ajax方式将参数提交到服务器进行验证,如何防止提交的请求不被抓包后串改,虽然无法说绝对安全却给非法提交提高了难度...

3869
来自专栏Ceph对象存储方案

Luminous版本PG 分布调优

Luminous版本开始新增的balancer模块在PG分布优化方面效果非常明显,操作也非常简便,强烈推荐各位在集群上线之前进行这一操作,能够极大的提升整个集群...

3145
来自专栏张善友的专栏

LINQ via C# 系列文章

LINQ via C# Recently I am giving a series of talk on LINQ. the name “LINQ via C...

2645
来自专栏码匠的流水账

聊聊NettyConnector的start及shutdown

reactor-netty-0.7.6.RELEASE-sources.jar!/reactor/ipc/netty/NettyConnector.java

851
来自专栏陈仁松博客

ASP.NET Core 'Microsoft.Win32.Registry' 错误修复

今天在发布Asp.net Core应用到Azure的时候出现错误InvalidOperationException: Cannot find compilati...

4868

扫码关注云+社区