有选择地从多级JSON对象中提取数据

时间:2013-02-14 05:09:56

标签: javascript json if-statement loops

我有一个多级JSON对象,其中包含143个其他对象的数组。

在此对象上运行console.log(obj):

0: Object
  ActFTEs: 0.00
  Actual: 11111
  Bud_Month: "October"
  FY_CD: 2013
  Mission_Name: "RST"
__proto__: Object
1: Object
  ActFTEs: 0.00
  Actual: 10000
  Bud_Month: "FY Total"
  FY_CD: 2013
  Mission_Name: "RST"

等....通过所有143个对象。但是,名称/值对Mission_Name:" RST"仅在第一个 n 对象中流行。

例如,obj 43包含:

43: Object
   ActFTEs: 0.00
   Actual: 10000
   Bud_Month: "FY Total"
   FY_CD: 2013
   Mission_Name: "VAO"

我创建了以下函数,但这仍然返回与" Bud_Month"相关的名称/值对的143个值的全部范围。

function get_dataArray() {
 var arr = [];
 var i= 0;
 for (i=0;i<jsonobj.row.length;i++) {   
    if (jsonobj.row[i][name]="RST") {
         arr[i] = jsonobj.row[i]["Bud_Month"];
    }    
 } 
 console.log(arr);
 return arr;
}

返回:

["October", "FY Total", "December", "January", "February", "March", "April", "May", "June", "July", "August", "September", "FY Total", "October", "November", "December", "January", "February", "March", "April", "May", "June", "July", "August", "September", "November", "October", "November", "December", "January", "February", "March", "April", "May", "June", "July", "August", "September", "FY Total", "October", "FY Total", "December", "January", "February", "March", "April", "May", "June", "July", "August", "September", "FY Total", "October", "November", "December", "January", "February", "March", "April", "May", "June", "July", "August", "September", "November", "October", "FY Total", "December", "January", "February", "March", "April", "May", "June", "July", "August", "September", "FY Total", "October", "November", "December", "January", "February", "March", "April", "May", "June", "July", "August", "September", "November", "October", "FY Total", "December", "January", "February", "March", "April", "May", "June"…]

有没有人对如何返回&#34; Bud_Month&#34;的值有任何建议?仅在包含Mission_Name的名称/值对的对象中:&#34; RST&#34; ?

1 个答案:

答案 0 :(得分:0)

if (jsonobj.row[i][name]="HST") {
     arr[i] = jsonobj.row[i]["Bud_Month"];
}   

应该是:

if (jsonobj.row[i][name]=="RST") {
     arr[i] = jsonobj.row[i]["Bud_Month"];
}