I'm trying to create some synthetic data which needs to be represented in this kind of structure (an array of arrays):
dataset = [
['Period', 'c1', 'c2', 'c3'],
['2018-01', 2000, 2002, 2004],
['2018-08', 3000, 3003, 3006],
['2019-01', 4000, 4002, 4008]
]
I've made a function to help:
function get_datapoints() {
dates = ['2018-01', '2018-08', '2019-01']
data = [];
data.push(['Period', 'c1', 'c2', 'c3'])
for (i in dates) {
sequence = []
for (val = 1; val < 4; val++) {
sequence.push(get_randomval())
}
data.push([dates[i], sequence.join()])
console.log(data)
}
return data
function get_randomval(min = 2000, max = 5000) {
let difference = max - min;
let rand = Math.random();
rand = Math.floor(rand * difference);
rand = rand + min;
return parseInt(rand);
}
This is generating data which looks like this:
[
['Period', 'c1', 'c2', 'c3']
['2018-01', '59797,47895,63209']
['2018-08', '28342,38450,70694']
['2019-01', '32348,44872,92501']
]
This understandably gives the error: Error: Row 1 has 2 columns, but must have 4
Clearly there are two 'fields' in each data array, each enclosed in a single quote pair. I'm pulling my hair out trying to figure out how to get those integers into the array without the single quotes. This data is actually to be used as part of a google.visualization.arrayToDataTable needed by Google Charts.
https://developers.google.com/chart/interactive/docs/gallery/linechart shows an example of this kind of structure (an array with both strings and ints).
How can I get those values into their arrays without the single quotes? Thanks!
data.push([dates[i], ...sequence])instead ofdata.push([dates[i], sequence.join()])jointurns your array into a string.letorconst, as appropriate.