第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

js,json樹查找父級路徑

js,json樹查找父級路徑

阿晨1998 2019-05-12 12:51:44
constdata=[{id:1,pid:0,text:1,children:[{id:2,pid:1,text:2,children:[{id:3,pid:2,text:3,children:[]}]}]}]//怎么查找id的父級的index索引id=3父級2所在數(shù)組index為0,在上父級的id1,所在數(shù)組的index0,直到最頂一層//輸入id3//返回[0,0,0]索引的數(shù)組
查看完整描述

2 回答

?
Qyouu

TA貢獻1786條經(jīng)驗 獲得超11個贊

constdata=[
{
id:1,
pid:0,
text:1,
children:[{
id:2,
pid:1,
text:2,
children:[{
id:3,
pid:2,
text:3,
children:[]
}]
}]
},
{
id:4,
pid:0,
text:4,
children:[{
id:5,
pid:4,
text:5,
children:[{
id:6,
pid:5,
text:6
}]
},{
id:7,
pid:4,
text:7,
children:[{
id:8,
pid:7,
text:8
}]
}]
}
]
functionfindIndexArray(data,id,indexArray){
letarr=Array.from(indexArray)
for(leti=0,len=data.length;iarr.push(i)
if(data[i].id===id){
returnarr
}
letchildren=data[i].children
if(children&&children.length){
letresult=findIndexArray(children,id,arr)
if(result)returnresult
}
arr.pop()
}
returnfalse
}
findIndexArray(data,3,[])//[0,0,0]
findIndexArray(data,8,[])//[1,1,0]
findIndexArray(data,7,[])//[1,1]
                            
查看完整回答
反對 回復 2019-05-12
  • 2 回答
  • 0 關注
  • 594 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號