2015-02-24 112 views
-1

我有一個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; 
 
}

這是工作,但我能夠使用該密鑰只得到第一個值...我需要所有該鍵的值。 。幫助我..

+1

使用'underscore'庫,它就像'linq'爲'javascript' – 2015-02-24 03:30:04

+1

份額,你已經嘗試......人們會更願意迴應,他們也可以提出其他建議的遞歸函數。 – 2015-02-24 03:43:03

+0

@JoelGregory ...我添加了我試過的遞歸函數.. – 2015-02-24 05:57:01

回答

0

這是我的問題的工作代碼...

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]); 
 
      } 
 
     } 
 
    } 
 

 
};