2017-09-14 111 views
-3

鍵值我有這樣的代碼在JavaScript獲取從JSON

我想在添加值「平均」鍵給數組標籤

有了這個代碼,我的陣列中得到了一個未定義

labels: string[] = []; 
dataSets: number[] = []; 

jsonData = [ 
    { 
     "pcnt12":13.05049839360738, 
     "pcnt23":38.76513716121592, 
     "pcnt34":40.069198451272754, 
     "pcnt45":8.115165993903945, 
     "avg":2.967567344921328 
    }, 
    { 
     "pcnt12":11.998029631313706, 
     "pcnt23":39.30733962335645, 
     "pcnt34":37.89852601265583, 
     "pcnt45":10.796104732674017, 
     "avg":3.0199537721192833 
    }, 
    { 
     "pcnt12":13.221410326877688, 
     "pcnt23":40.50486598338129, 
     "pcnt34":36.13518057486501, 
     "pcnt45":10.138543114876006, 
     "avg":2.9832600472048885 
    } 
] 


for(let i:any = 0; i < this.jsonData.length; i++) { 
    for (let key in this.jsonData[i]) { 
     this.labels.push(key["avg"]); 
    } 
} 
+0

您應該提及您正在嘗試的語言 –

+1

以及出現了什麼問題? – Plog

回答

1
for(let i:any = 0; i < this.jsonData.length; i++) { 
     this.labels.push(jsonData[i].avg.toString()); 
    } 
} 

這應該有效。