{ "name": "第一层", index:1, "children":[ { "name":"第二层", index:2, },{ "name":"第二层", index:2, "children":[ { "name":"第三层", index:3, },{ "name":"第三层", index:3, }, ] } ] } 想在这个树状图中添加他们每一层的index,
1 回答
冉冉说
TA贡献1877条经验 获得超1个赞
最终写法。
function fun(arry){
for(let i in arry){
let menu = arry[i];
if(!menu['num']){
menu['num'] = 1;
}
if(menu['children'].length && menu['children']){
for(let j in menu['children']){
menu['children'][j]['num'] = arry[i]['num'] + 1;
fun(menu['children'])
}
}
}
}
添加回答
举报
0/150
提交
取消