在Chart.js中绘制对象

时间:2018-04-23 17:31:09

标签: javascript json chart.js

我在数组中有一个对象(已由API返回)。说,

data = [{"A":"40","B":"37","C":"31.5"}]

如何在“label”数组中推送A,B,C,D以及Chart.js中“dataset”数组中的值?

1 个答案:

答案 0 :(得分:1)

使用Object.keys()Object.values()

这些功能是不言自明的



var data = [{"A":"40","B":"37","C":"31.5"}]

// If you know the property key you want to delete use 
// delete data[0]["A"]

//If you dont know it 
//delete data[0][Object.keys(data[0])[0]]

var labels = Object.keys(data[0]);
var dataset = Object.values(data[0])

console.log("labels: ", labels)

console.log("dataset: ", dataset)