在jQuery中搜索JSON对象数组中的元素
问题描述:
我有一个JSON对象数组,其中这些对象也可能是JSON对象数组。现在,我需要动态地找到一个值,因为对象的no:不会在数组中相同。例如,在jQuery中搜索JSON对象数组中的元素
[
{
"Plan": {
"Node Type": "CTE Scan",
"CTE Name": "tab",
"Alias": "tab",
"Startup Cost": 756.66,
"Total Cost": 761.16,
"Plan Rows": 67,
"Plan Width": 12,
"Filter": "(sum > 10000)",
"Plans": [
{
"Node Type": "Aggregate",
"Strategy": "Sorted",
"Parent Relationship": "InitPlan",
"Subplan Name": "CTE tab",
"Startup Cost": 471.70,
"Total Cost": 756.66,
"Plan Rows": 200,
"Plan Width": 8,
"Plans": [
{
"Node Type": "Merge Join",
"Parent Relationship": "Outer",
"Join Type": "Inner",
"Startup Cost": 471.70,
"Total Cost": 684.82,
"Plan Rows": 13968,
"Plan Width": 8,
"Merge Cond": "(p.pr_id = ep.pr_id)",
"Plans": [
{
"Node Type": "Sort",
"Parent Relationship": "Outer",
"Startup Cost": 51.37,
"Total Cost": 53.17,
"Plan Rows": 720,
"Plan Width": 4,
"Sort Key": ["p.pr_id"],
"Plans": [
{
"Node Type": "Seq Scan",
"Parent Relationship": "Outer",
"Relation Name": "product",
"Alias": "p",
"Startup Cost": 0.00,
"Total Cost": 17.20,
"Plan Rows": 720,
"Plan Width": 4
}
]
},
{
"Node Type": "Sort",
"Parent Relationship": "Inner",
"Startup Cost": 420.33,
"Total Cost": 430.03,
"Plan Rows": 3880,
"Plan Width": 8,
"Sort Key": ["ep.pr_id"],
"Plans": [
{
"Node Type": "Hash Join",
"Parent Relationship": "Outer",
"Join Type": "Inner",
"Startup Cost": 19.00,
"Total Cost": 189.05,
"Plan Rows": 3880,
"Plan Width": 8,
"Hash Cond": "(ep.emp_id = e.emp_id)",
"Plans": [
{
"Node Type": "Seq Scan",
"Parent Relationship": "Outer",
"Relation Name": "employee_vs_product",
"Alias": "ep",
"Startup Cost": 0.00,
"Total Cost": 29.40,
"Plan Rows": 1940,
"Plan Width": 12
},
{
"Node Type": "Hash",
"Parent Relationship": "Inner",
"Startup Cost": 14.00,
"Total Cost": 14.00,
"Plan Rows": 400,
"Plan Width": 12,
"Plans": [
{
"Node Type": "Seq Scan",
"Parent Relationship": "Outer",
"Relation Name": "employee",
"Alias": "e",
"Startup Cost": 0.00,
"Total Cost": 14.00,
"Plan Rows": 400,
"Plan Width": 12
}
]
}
]
}
]
}
]
}
]
}
]
}
}
]
在这里,我需要钥匙“Relaion名称”寻找价值......有三个值与关键,我们不知道有没有:存在于它的对象..所以我需要一个解决方案来动态地使用该密钥来查找值。
我试着编码一个递归函数,但我不成功..所以帮助我,如果你知道它。
递归函数我想:
var result1=searchInPlan(jsonarray[0].Plan);
function searchInPlan(node)
{
\t var result='';
\t {
\t \t if(node.Plans == undefined)
\t \t {
\t \t \t if(node["Relation Type"] != undefined)
\t \t \t \t result += node["Relation Name"];
\t \t \t return result;
\t \t }
\t \t else
\t \t {
\t \t \t return result+searchInPlans(node.Plans);
\t \t }
\t }
}
function searchInPlans(nodes)
{
\t console.log("bollo",nodes);
\t var result = '';
\t for(j=0;j<nodes.length;j++)
\t {
\t \t result += searchInPlan(nodes[j]);
\t }
\t return result;
}
这是工作,但我能够使用该密钥只得到第一个值...我需要所有该键的值。 。帮助我..
答
这是我的问题的工作代码...
var x=[];
var y=[];
var i1=0;
var j1=0;
function scan(obj) {
var k;
if (obj instanceof Object) {
for (k in obj) {
if (obj.hasOwnProperty(k)) {
if(k == "Relation Name")
\t \t \t \t {
\t \t \t \t \t x[i1]= obj[k];
\t \t \t \t \t i1++;
\t \t \t \t \t
\t \t \t \t }
scan(obj[k]);
}
}
}
};
使用'underscore'库,它就像'linq'为'javascript' – 2015-02-24 03:30:04
份额,你已经尝试......人们会更愿意回应,他们也可以提出其他建议的递归函数。 – 2015-02-24 03:43:03
@JoelGregory ...我添加了我试过的递归函数.. – 2015-02-24 05:57:01