John Jones
John Jones

Reputation: 259

Pushing data into array with JavaScript

I'm relatively new to JavaScript and I've been having trouble pushing data into an array.

I have two dynamic vars and I need the array to be formatted like:

var array = [[-373597200000, 315.71], [-370918800000, 317.45], [-368326800000, 317.50]];

I already have a loop running for each iteration of the vars, I'm just not sure how I put the two vars into the array in the format above. I've tried:

array.push(var1 + "," + var2);

For each iteration of the loop but it doesn't seem to be working.

So, what's the proper way to push data into an array in the format above?

Thanks in advance!

Upvotes: 4

Views: 10389

Answers (4)

Summy
Summy

Reputation: 807

var a = new Array(); a.push('Test');

output :

["Test"]

Actually I tried your example it worked for me

var a = new Array(); var var1 = "Test1"; var var2 = "Test2"; a.push(var1 + " "+ var2); a;

output : ["Test1 Test2"]

Upvotes: 0

Nosredna
Nosredna

Reputation: 86196

array.push([var1,var2]);

Upvotes: 1

Jonathan Feinberg
Jonathan Feinberg

Reputation: 45324

array.push([var1,var2])

Upvotes: 1

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827316

Your array contains other arrays as elements, you need to add another array, not a string:

array.push([var1, var2]);

More info:

Upvotes: 5

Related Questions