auditore
auditore

Reputation: 47

Plotting objects in Chart.js

I have an object in an array (which has been returned by an API). Say,

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

How do I push A,B,C,D in the "label" array and the values in the "dataset" array in Chart.js ?

Upvotes: 1

Views: 244

Answers (1)

Use Object.keys() and Object.values()

Those functions are self explanatory

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)

Upvotes: 1

Related Questions