1

I'm trying to push multiple values into an array.

When I use:

csvData.push('data[0][index],data[1][index],data[2][index],data[3][index]');

it formats it corrects so that

csvData[0] = "data[0][index],data[1][index],data[2][index],data[3][index]"
csvData[1] = "data[0][index],data[1][index],data[2][index],data[3][index]"
etc

Also when it evolves into a csv, it is correct in the 4 columns.

However when I use no quotes:

csvData.push(data[0][index],data[1][index],data[2][index],data[3][index]); 

I get the values I want, but it's single dimensional.

csvData[0] = 23
92
74
22
etc

instead of

csvData[0] = 23,92,74,22

How can I add values it correctly?

1
  • 1
    What do you want the value of each element of csvData to be? An array? A string? Commented Jun 3, 2016 at 17:06

3 Answers 3

2

You can create an array and push that:

csvData.push( [ data[0][index],data[1][index],data[2][index],data[3][index] ] ); 

The added brackets ([ ]) around the list of values creates an array, and that in turn is what's pushed onto the csvData array.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh, Added the brackets and we're good. I tried join() and that also worked. Thx!
2

Put them inside an array and join them using join() method then push it.

csvData.push([data[0][index],data[1][index],data[2][index],data[3][index]].join()); 

1 Comment

@homersheineken : glad to help
1

if you want multidimentional than use

csvData.push([data[0][index],data[1][index],data[2][index],data[3][index]]);

Comments

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.