{ "name": "第一層", index:1, "children":[ { "name":"第二層", index:2, },{ "name":"第二層", index:2, "children":[ { "name":"第三層", index:3, },{ "name":"第三層", index:3, }, ] } ] } 想在這個(gè)樹狀圖中添加他們每一層的index,
1 回答

冉冉說
TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個(gè)贊
最終寫法。
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'])
}
}
}
}
添加回答
舉報(bào)
0/150
提交
取消