递归搜索JSON或Javascript对象 [英] Recursively Search in JSON or Javascript Object
问题描述
例如:
[{
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网站编写的。我正在尝试生成其他内容的导航,并且需要能够:
You'll notice that you could do json[1].subpages[0].subpages[0]
but I don't know how deep it's going to be. This is written by a designer client of mine for an AJAX site he's building for a client. I'm trying to generate a navigation amongst other things and need to be able to:
A。递归解析以构建导航(< ul>< li>< a> ...
)
A. Parse this recursively to build a navigation (<ul><li><a>...
)
B中。搜索匹配的ID。像这样(但这不是递归的)[并忽略中的
,仅仅是为了示例)
B. Search for a matching id. Like this (but this isn't recursive)[and ignore the for...in
, its just for example's sake)
var matchId(id,json){
for(x in json){
if(json[x].id == id){ var theMatch = json[x]; break; }
}
}
推荐答案
此代码为您构建导航:
function buildNavForNode(node) {
var result = "<li id='" + node.id + "'><a href='" + node.slug + "'>" + node.title + "</a>";
if(node.subpages == undefined) {
return result + "</li>";
} else {
return result + buildNavForNodes(node.subpages) + "</li>";
}
}
function buildNavForNodes(nodes) {
var result = "<ul>";
var i = 0;
var len = nodes.length;
for(; i < len; i++) {
result += buildNavForNode(nodes[i]);
}
return result + "</ul>";
}
以下是您使用它的方式:
Here's how you'd use it:
var testData = [
{
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'
},
]
}
]
}
];
$(function(){
htmlToInsert = buildNavForNodes(testData);
console.log(htmlToInsert);
$('body').html(htmlToInsert);
});
您可以使用递归函数轻松完成此操作,但我认为这很好地描述了职责分离在弄清楚如何处理页面的集合和处理单个页面之间。
You can do this quite readily with a recursive function, but I think this nicely delineates the separation of duties between figuring out what to do with a collection of pages and processing a single page itself.
这篇关于递归搜索JSON或Javascript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!