0

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!

5
  • 5
    Use data.push([dates[i], ...sequence]) instead of data.push([dates[i], sequence.join()]) Commented Apr 18, 2022 at 13:57
  • join turns your array into a string. Commented Apr 18, 2022 at 14:00
  • Fantastic! All working now! Spread operator huh? Thats a new one to me. Commented Apr 18, 2022 at 14:04
  • You definitely should be declaring your variables with let or const, as appropriate. Commented Apr 18, 2022 at 14:13
  • Thanks @Pointy, yes, I'm slowly getting back up to speed with Js now and appreciate the observation and suggestion. Commented Apr 19, 2022 at 17:41

3 Answers 3

1

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])
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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)

1 Comment

Thanks you @kabanus this makes a lot of sense. I like simple!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.