从json

时间:2017-09-14 10:48:43

标签: json

我在javascript中有这段代码

我想在" avg"中添加值数组标签的关键

使用此代码我在数组中未定义

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

1 个答案:

答案 0 :(得分:1)

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

这应该有用。