If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Hybrid View

2D arrays in JavaScrpit? [SOLVED]

I am writing a program in which it would be very useful to use a 2D array, but I have not been able to find code for a 2D array, and I'm starting to doubt that they exist in JavaScript. If they don't, what is the easiest way to script one? An array of arrays seems rather large and unwieldy...
Thanks, PW

Update: Figured it out, apparently the book i was using isn't current...

First you create and array, then you create the first element as an array, and then insert two elements into it:

Code:

var myarray = new Array();
myarray[0] = new Array(2); // Make the first element an array of two elements
myarray[0,0] = 'first'; //populate the first part of the first element of myarray
myarray[0,1] = 'second'; //populate the second part of the first element of myarray

javascript 2d arrays are a sham

From my experience there are no 2D arrays. The above code works, but once you try to add a second element to the array, the first one is lost. Eg:

Code:

var myarray = new Array();
myarray[0] = new Array(2); // Make the first element an array of two elements
myarray[0,0] = 'first'; //populate the first part of the first element of myarray
myarray[0,1] = 'second'; //populate the second part of the first element of myarray
myarray[1] = new Array(2);
myarray[1,0] = 'santa';
myarray[1,1] = 'claus';
alert("myarray[0,0] should be 'first' but it is: " + myarray[0,0]); //displays 'santa'
alert("myarray[0,1] should be 'second' but it is: " + myarray[0,1]); //displays 'claus'

From my experience there are no 2D arrays. The above code works, but once you try to add a second element to the array, the first one is lost. Eg:

Code:

var myarray = new Array();
myarray[0] = new Array(2); // Make the first element an array of two elements
myarray[0,0] = 'first'; //populate the first part of the first element of myarray
myarray[0,1] = 'second'; //populate the second part of the first element of myarray
myarray[1] = new Array(2);
myarray[1,0] = 'santa';
myarray[1,1] = 'claus';
alert("myarray[0,0] should be 'first' but it is: " + myarray[0,0]); //displays 'santa'
alert("myarray[0,1] should be 'second' but it is: " + myarray[0,1]); //displays 'claus'

I just created 2 arrays, instead.

I think for example [1][0] should be used instead of [1,0]

Code:

var myarray = new Array();
myarray[0] = new Array(2); // Make the first element an array of two elements
myarray[0][0] = 'first'; //populate the first part of the first element of myarray
myarray[0][1] = 'second'; //populate the second part of the first element of myarray
myarray[1] = new Array(2);
myarray[1][0] = 'santa';
myarray[1][1] = 'claus';
alert(myarray[0][0]); //displays 'first'
alert(myarray[0][1]); //displays 'second'

(This is an array of arrays though; it can be jagged, not really that kind of "rectangular" 2D arrays)