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
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'
var my2D =
["first","santa"]
,["second","claus"]
];
alert(my2D = my2D[0][0])
Whether 2D arrays exist or not is a matter of opinion, I suppose. A Javascript 2D array can be "ragged". But then again JS arrays can't be compared to arrays in say Java anyway.
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)
I just noticed too, when i was trying to implement a 2D array into JS. It is Screwed. But here is a solution if you want to stick to multidimensional arrays: An Object.
function Array2D() {
function setArray(length) { // set an array inside the object
this.elem=new Array();
for(i=0;i<length;i++) {
this.elem[i]=null;
}
}
this.setArray=setArray;
this.elem=null; // our array
}
With this little fella you can hide your intentions from the Evil Javascript Developers, and secretly make a 2D array:
arr = new Array();
arr[0] = new Array2D();
arr[0].setArray(2);
arr[0].elem[0] = "Hy, ";
arr[0].elem[1] = "I'm ";
arr[1] = new Array2D();
arr[1].setArray(2);
arr[1].elem[0] = "Da";
arr[1].elem[1] = "vid!";
alert(arr[0].elem[0] + arr[0].elem[1] + arr[1].elem[0] + arr[1].elem[1]); // this will say "Hy, I'm David!"
Last edited by DavidAdama; 02-01-2008 at 01:12 PM.
Bookmarks