javascript array生成 目录树

时间:2022-06-17 12:33:00
我在数据库得到这么一个数组,想要通过生成三层目录树,用javascript怎么组织啊?
数据库返回信息
[{"child_id":[3,4,17,2],"id":1,"name":"全部"},{"child_id":[],"id":3,"name":"内置"},{"child_id":[11],"id":4,"name":"其他商品"},{"child_id":[],"id":11,"name":"原材料"},{"child_id":[],"id":17,"name":"Pet Toys"},{"child_id":[8,9,5,7,6,10],"id":2,"name":"可销售"},{"child_id":[],"id":8,"name":"配件"},{"child_id":[],"id":9,"name":"组件"},{"child_id":[],"id":5,"name":"电脑"},{"child_id":[],"id":7,"name":"外置设备"},{"child_id":[],"id":6,"name":"服务"},{"child_id":[],"id":10,"name":"软件"},{"child_id":[16,15,13,14],"id":12,"name":"苹果产品"},{"child_id":[],"id":16,"name":"苹果配件"},{"child_id":[],"id":15,"name":"iMac电脑"},{"child_id":[],"id":13,"name":"iPad"},{"child_id":[],"id":14,"name":"iPod"}]


最终需要的数据
[{"child_id":[{"child_id":[],"id":3,"name":"内置"},{"child_id":[{"child_id":[],"id":11,"name":"原材料"}],"id":4,"name":"其他商品"},{"child_id":[],"id":17,"name":"Pet Toys"},{"child_id":[{"child_id":[],"id":8,"name":"配件"},{"child_id":[],"id":9,"name":"组件"},{"child_id":[],"id":5,"name":"电脑"},{"child_id":[],"id":7,"name":"外置设备"},{"child_id":[],"id":6,"name":"服务"},{"child_id":[],"id":10,"name":"软件"}],"id":2,"name":"可销售"}],"id":1,"name":"全部"},{"child_id":[{"child_id":[],"id":16,"name":"苹果配件"},{"child_id":[],"id":15,"name":"iMac电脑"},{"child_id":[],"id":13,"name":"iPad"},{"child_id":[],"id":14,"name":"iPod"}],"id":12,"name":"苹果产品"}]

3 个解决方案

#1


有没有人有解决办法啊

#2


var json=[{"child_id":[3,4,17,2],"id":1,"name":"全部"},{"child_id":[],"id":3,"name":"内置"},{"child_id":[11],"id":4,"name":"其他商品"},{"child_id":[],"id":11,"name":"原材料"},{"child_id":[],"id":17,"name":"Pet Toys"},{"child_id":[8,9,5,7,6,10],"id":2,"name":"可销售"},{"child_id":[],"id":8,"name":"配件"},{"child_id":[],"id":9,"name":"组件"},{"child_id":[],"id":5,"name":"电脑"},{"child_id":[],"id":7,"name":"外置设备"},{"child_id":[],"id":6,"name":"服务"},{"child_id":[],"id":10,"name":"软件"},{"child_id":[16,15,13,14],"id":12,"name":"苹果产品"},{"child_id":[],"id":16,"name":"苹果配件"},{"child_id":[],"id":15,"name":"iMac电脑"},{"child_id":[],"id":13,"name":"iPad"},{"child_id":[],"id":14,"name":"iPod"}];
var d=[];
for(var i=0;i<json.length;i++){
for(var j=0;j<json[i].child_id.length;j++){
d.push(json[i].child_id[j]);
json[i].child_id[j]=(function(a){for(var i=0;i<json.length;i++)if(json[i].id==a)return json[i]})(json[i].child_id[j])
}
}
var n=[];
for(var i=0;i<json.length;i++)if(d.indexOf(json[i].id)==-1)n.push(json[i])
alert(JSON.stringify(n))

#3


楼主能把目录树的规则说一下吗,只发个code难道让观众自己从中总结规律

#1


有没有人有解决办法啊

#2


var json=[{"child_id":[3,4,17,2],"id":1,"name":"全部"},{"child_id":[],"id":3,"name":"内置"},{"child_id":[11],"id":4,"name":"其他商品"},{"child_id":[],"id":11,"name":"原材料"},{"child_id":[],"id":17,"name":"Pet Toys"},{"child_id":[8,9,5,7,6,10],"id":2,"name":"可销售"},{"child_id":[],"id":8,"name":"配件"},{"child_id":[],"id":9,"name":"组件"},{"child_id":[],"id":5,"name":"电脑"},{"child_id":[],"id":7,"name":"外置设备"},{"child_id":[],"id":6,"name":"服务"},{"child_id":[],"id":10,"name":"软件"},{"child_id":[16,15,13,14],"id":12,"name":"苹果产品"},{"child_id":[],"id":16,"name":"苹果配件"},{"child_id":[],"id":15,"name":"iMac电脑"},{"child_id":[],"id":13,"name":"iPad"},{"child_id":[],"id":14,"name":"iPod"}];
var d=[];
for(var i=0;i<json.length;i++){
for(var j=0;j<json[i].child_id.length;j++){
d.push(json[i].child_id[j]);
json[i].child_id[j]=(function(a){for(var i=0;i<json.length;i++)if(json[i].id==a)return json[i]})(json[i].child_id[j])
}
}
var n=[];
for(var i=0;i<json.length;i++)if(d.indexOf(json[i].id)==-1)n.push(json[i])
alert(JSON.stringify(n))

#3


楼主能把目录树的规则说一下吗,只发个code难道让观众自己从中总结规律