Declare an empty two-dimensional array in Javascript? -
i want create 2 dimensional array in javascript i'm going store coordinates (x,y). don't know yet how many pairs of coordinates have because dynamically generated user input.
example of pre-defined 2d array:
var arr=[[1,2],[3,4],[5,6]];
i guess can use push method add new record @ end of array.
how declare empty 2 dimensional array when use first arr.push() added index 0, , every next record written push take next index?
this easy do, i'm newbie js, , appreciate if write short working code snippet examine. thanks
you can declare regular array so:
var arry = [];
then when have pair of values add array, need is:
arry.push([value_1, value2]);
and yes, first time call arry.push
, pair of values placed @ index 0.
from nodejs repl:
> var arry = []; undefined > arry.push([1,2]); 1 > arry [ [ 1, 2 ] ] > arry.push([2,3]); 2 > arry [ [ 1, 2 ], [ 2, 3 ] ]
of course, since javascript dynamically typed, there no type checker enforcing array remains 2 dimensional. have make sure add pairs of coordinates , not following:
> arry.push(100); 3 > arry [ [ 1, 2 ], [ 2, 3 ], 100 ]
Comments
Post a Comment