1. 程式人生 > >將扁平數據轉換為tree型數據

將扁平數據轉換為tree型數據

轉換 asc light () n) type brush 2-2 ons

<script type="text/javascript">
			var meun_list = [
				{
					id:‘1‘,
					meun_name:‘設置‘,
					meun_url:‘‘,
					parent_id:0
				},
				{
					id:‘1-1‘,
					meun_name:‘權限設置‘,
					meun_url:‘‘,
					parent_id:‘1‘
				},
				{
					id:‘1-1-1‘,
					meun_name:‘用戶管理列表‘,
					meun_url:‘‘,
					parent_id:‘1-1‘
				},
				{
					id:‘1-1-2‘,
					meun_name:‘用戶管理新增‘,
					meun_url:‘‘,
					parent_id:‘1-1‘
				},
				{
					id:‘1-1-3‘,
					meun_name:‘角色管理列表‘,
					meun_url:‘‘,
					parent_id:‘1-1‘
				},
				{
					id:‘1-2‘,
					meun_name:‘菜單設置‘,
					meun_url:‘‘,
					parent_id:‘1‘
				},
				{
					id:‘1-2-1‘,
					meun_name:‘菜單列表‘,
					meun_url:‘‘,
					parent_id:‘1-2‘
				},
				{
					id:‘1-2-1‘,
					meun_name:‘菜單添加‘,
					meun_url:‘‘,
					parent_id:‘1-2‘
				},
				{
					id:‘2‘,
					meun_name:‘訂單‘,
					meun_url:‘‘,
					parent_id:0
				},
				{
					id:‘2-1‘,
					meun_name:‘報單審核‘,
					meun_url:‘‘,
					parent_id:‘2‘
				},
				{
					id:‘2-2‘,
					meun_name:‘退款管理‘,
					meun_url:‘‘,
					parent_id:‘2‘
				}
			];
			
			function buildTree(list){
				var temp = {},tree = {};
				for(var i in list){
					temp[list[i].id] = list[i]
				}
				console.log(temp)
				for(var i in temp){
					if(temp[i].parent_id){
						if(!temp[temp[i].parent_id].children){
							temp[temp[i].parent_id].children = new Object();
						}
						temp[temp[i].parent_id].children[temp[i].id] = temp[i]
					}else{
						tree[temp[i].id] = temp[i]
					}
				}
				console.log(tree)
			}
			buildTree(meun_list);
		</script>

將扁平數據轉換為tree型數據