1

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 ?

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to skip the first property?
delete data[0]["A"]
Or delete data[0][Object.keys(data[0])[0]]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.