我正在尝试访问JSON响应中的键和值以填充数组。该数组当前为空,用于chart.js。 API:https://api.coindesk.com/v1/bpi/historical/close.json具有每24小时更改一次的键和值,即:

{"bpi":
  {"2017-10-06":4370.245,
  "2017-10-07":4437.0338,
  }
}


数组“数据”需要填充在component.ts文件中,而不是模板或.html文件中

chartData = [
    { data: [], label: 'Bitcoin' },
  ];


为了澄清,我试图用诸如“ 4370.245”之类的值填充数组“数据”,这些值来自上述URL的http.get请求。

最佳答案

尝试这个

chartData = [
    { data: [], label: 'Bitcoin' },
  ];

let arrayData = chartData[0].data;//this will return you data inside chartData


根据文章编辑

    var outputData = []

    var keysArray = Object.keys(tmpObj.bpi)
    for (var key in tmpObj.bpi) {
        if (tmpObj.bpi.hasOwnProperty(key )) {
            outputData.push(tmpObj.bpi[key])
        }
    }
    console.log(outputData);




var tmpObj = {"bpi":
  {"2017-10-06":4370.245,
  "2017-10-07":4437.0338,
  }
};

var outputData = []

var keysArray = Object.keys(tmpObj.bpi)
for (var key in tmpObj.bpi) {
if (tmpObj.bpi.hasOwnProperty(key )) {
	outputData.push(tmpObj.bpi[key])
}
}
console.log(outputData);

10-07 21:59