Formatting Data For Google Charts
I am trying to achieve something in order to get my data formatted correctly for Google Charts. At the moment, I have data in the following format Array(4) 0: {Category: 'cat2
Solution 1:
You can use forEach
on any of the the arrays and then create an array with three values with corresponding values.
let myArray = [];
const header = ["Category", "Count", { role: 'style' }];
const data = [
{"Category":"cat3","Count":59},
{"Category":"cat1","Count":109},
{"Category":"cat2","Count":120},
{"Category":"cat4","Count":57}
]
const obj = data.reduce((ac,{Category, Count}) => (ac[Category] = Count,ac),{});
const categories = ["cat1", "cat2", "cat3", "cat4"];
const count = [57, 11, 52, 24];
const colors = ["red", "blue", "silver", "yellow"];
myArray.push(header);
categories.forEach((x,i) => {
myArray.push([x,obj[x],colors[i]]);
})
console.log(myArray)
Post a Comment for "Formatting Data For Google Charts"