我在数组中有一个对象(已由API返回)。说,
data = [{"A":"40","B":"37","C":"31.5"}]
如何在“label”数组中推送A,B,C,D以及Chart.js中“dataset”数组中的值?
答案 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)