Home > Blockchain >  Javascript add integers to mixed array
Javascript add integers to mixed array

Time:04-19

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!

CodePudding user response:

The problem is sequence.join() This method is used for strings and when no arguments are provided it converts the elements in the array to a string with a , as delimiter. That's why you get the string.

You could use the spread operator instead, this will behave as you inteded to.

data.push([dates[i], ...sequence])

CodePudding user response:

instead of using join here data.push([dates[i], sequence.join()]) (which converts it to a string) use the spread operator like this data.push([dates[i], ...sequence]).

Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

CodePudding user response:

You are explicitly converting to a string using sequence.join. You should instead pass each sequence item as a separate argument. I see answers suggesting the nifty unpacking operator:

[dates[i], ...sequence]

but there are older solutions which may be more readable such as

data.push([dates[i]].concat(sequence))

or using unshift to prepend dates[i] (costs an extra line), or my favorite, just initialize the sequence like you want it:

sequence = [dates[i]]

leave the code as is, and then just

data.push(sequence)
  • Related