I have data in the following format:
[
{
"id": 1,
"cost": 323,
"comparison": "High",
"flag": "Alert"
}
...
]
I am trying to prepare this data for Google charts, more specifically to make a Histogram. The format required is like on next example:
var data = google.visualization.arrayToDataTable([
['Name', 'Number'],
['Name 1', number1],
['Name 2', number2],
['Name 3', number3],
...
]);
So, from my input data, I want to use the id and cost properties. To approach this, I have implemented next code:
const keys = Object.keys(this.data[0]).filter(e => !(["comparison","flag"].includes(e)));
const values = this.data.map(e => Object.entries(e).filter(([k]) => keys.includes(k)).map(([, v]) => v));
const formattedArray = [keys, ...values];
This somewhat gives me the data I require, I am seeing something like this:
var data = google.visualization.arrayToDataTable([
['id', 'cost'],
[1, 323]
...
]);
But my chart is not displaying correctly, and the problem I think I am facing is that I am passing id's as an int, whereby Google charts expects column one to be a string.
How can I convert all the values that are placed into column one, so they are strings?
Thanks!
const keys = Object.keys(this.data[0]).filter(e => !(["comparison","flag"].includes(e)));to just get an array like["id", "cost"]. Why not just declare it like that explicitly? Also, using that approach, and in the case the first object of the array have otherkeysbesidesidandcost, those will be included on thekeysarray.