How to convert json to array in javascript for chart.js?

by aniya.jaskolski , in category: Javascript , 9 months ago

How to convert json to array in javascript for chart.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cali_green , 9 months ago

@aniya.jaskolski 

To convert a JSON object to an array in JavaScript for Chart.js, you can use the Object.values() method. Here is an example:

1
2
3
4
5
6
7
8
9
const jsonData = {
  "label1": 10,
  "label2": 20,
  "label3": 30
};

const dataArray = Object.values(jsonData);

console.log(dataArray); // [10, 20, 30]


In this example, the jsonData object is a JSON object with key-value pairs representing labels and corresponding data values. To convert it to an array, we use the Object.values() method which returns an array of the values from each key-value pair in the object. The resulting array is then stored in the dataArray variable.


You can then use this dataArray in your Chart.js configuration or data object as required.