-1

I have created an empty array, and I'd like to create and append arrays within this array:

values: []

I want to have the values array in a format like this:

[ [ 1025409600000 , 23.041422681023] , [ 1028088000000 , 19.854291255832] , [ 1030766400000 , 21.02286281168] ]

So I did

values.push({x: 1025409600000, y: 23.041422681023});

This creates a javascript object instead of an array of arrays. How can I not append a new javascript object but append an array within the values array instead?

caramelslice
  • 359
  • 1
  • 4
  • 16
  • 2
    If you don’t want an object to be pushed… don’t push an object, maybe? – Sebastian Simon Apr 17 '16 at 23:37
  • Possible duplicate of [How can I create a two dimensional array in JavaScript?](http://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript) – GG. Apr 17 '16 at 23:40

3 Answers3

4

just push array =)

values.push([1025409600000, 23.041422681023]);
Rudolf Manusachi
  • 2,311
  • 2
  • 19
  • 25
1

just iterate through your list of x and y coordinates and push them into their own array and then push each of those arrays into the parent (values) array. The following is obviously wrong and you need to iterate via loops, but this will give you an array called values, which is made up of other arrays of x and y coordinates.

var values = [];
var coordinates=[];
var x=1025409600000;
var y=23.041422681023;

coordinates.push(x);
coordinates.push(y);
//leads to coordinates being ['1025409600000','23.041422681023']

values.push(coordinates);

//leads to values being [['1025409600000 ','23.041422681023']]

and then you repeat to ensure that values is [[x,y],[x,y],[x,y]...]

gavgrif
  • 15,194
  • 2
  • 25
  • 27
0

This is because you're pushing objects {} into the array. You need to push arrays, so something like this:

values.push([obj.x, obj.y]);
timolawl
  • 5,434
  • 13
  • 29