【发布时间】:2016-06-04 17:55:14
【问题描述】:
您好,我有一个以下格式的 json 数组。
{
"data": {
"title": "MainNode"
},
"children": [{
"data": {
"title": "Firstchild",
"description": "Texttexttext"
},
"children": [{
"data": {
"title": "Firstchildschild",
"description": "Texttexttext"
},
"children": [{
"data": {
"title": "FirstchildschildsChild",
"description": "Texttexttext"
}
}]
}, {
"data": {
"title": "FirstchildsSecondchild",
"description": "Texttexttext"
},
"children": [{
"data": {
"title": "FirstchildsSecondchildsChild",
"description": "Texttexttext"
}
}]
}]
}]
}
我必须根据标题文本值读取数组并列出该特定数据及其子数据。
例如。如果我的输入(标题值将从 http URL 获取,如下所示)是 "Firstchild/Firstchildschild/",那么我必须列出 FirstchildschildsChild 及其描述。
如果我的输入是 /Firstchild/,那么我必须列出 Firstchildschild & FirstchildsSecondchild 数据及其子名称。
使用 jquery 如何获取我上面提到的记录。请告知处理此 json 而不是使用大量循环的最佳方法?
【问题讨论】:
-
jQuery 不提供任何帮助器来访问嵌套对象。你可能想看看Access / process (nested) objects, arrays or JSON
-
除了使用
Array.prototype方法会在内部执行这些循环外,总会涉及到循环。请尝试一下,当它不起作用时,请向我们展示该代码。或在codereview.stackexchange.com 上发布工作代码,询问改进方法
标签: javascript jquery arrays json tree