[{
id:'our-purpose',
title:'Our Purpose',
slug:'/our-purpose',
backgroundImage:'images/bg-our-purpose.jpg',
showInNav:1
},
{
id:'our-people',
title:'Our People',
slug:'/our-people',
backgroundImage:'images/bg-our-people.jpg',
showInNav:1,
subpages:[
{
id:'attorneys',
title:'Attorneys',
slug:'/our-people/attorneys',
subpages:[
{
id:'attorneys-cdb',
title:'Attorneys - Carla DeLoach Bryant',
slug:'/our-people/attorneys/carla'
},
{
id:'attorneys-jad',
title:'Attorneys - Jordan A. DeLoach',
slug:'/our-people/attorneys/jordan'
},
{
id:'attorneys-shh',
title:'Attorneys - Sarah H. Hayford',
slug:'/our-people/attorneys/sarah'
},
{
id:'attorneys-jsp',
title:'Attorneys - Jason S. Palmisano',
slug:'/our-people/attorneys/jason'
},
{
id:'attorneys-ldw',
title:'Attorneys - Lindsey DeLoach Wagner',
slug:'/our-people/attorneys/carla'
},
]
},
{
id:'legal-support',
title:'Legal Support',
slug:'/our-people/legal-support',
subpages:[
{
id:'legal-support-tb',
title:'Legal Support - Theolyn Brock',
slug:'/our-people/attorneys/theolyn'
},
{
id:'legal-support-cd',
title:'Legal Support - Cheri DeFries',
slug:'/our-people/attorneys/cheri'
},
]
},
//...and so on
你會發現,你可以做json[1].subpages[0].subpages[0]
但我不知道這是怎麼回事有多深是。這是由我的一位設計客戶爲他爲客戶建立的AJAX網站編寫的。我試圖生成除其他事項外導航,需要能夠:
A.解析這個遞歸構建導航(<ul><li><a>...
)
B.搜索一個匹配的ID。這樣的(但不是遞歸),而忽略了for...in
,它只是舉例的緣故)
var matchId(id,json){
for(x in json){
if(json[x].id == id){ var theMatch = json[x]; break; }
}
}
我不真的在這裏看到一個問題。你似乎明白你需要怎麼做。 – icktoofay
...?嗯,我不知道如何使這個遞歸 –