views:

4037

answers:

8

I have been reading online and some places say it isnt possible, some say it is and then give an example and others refute the example, etc.

  1. How do I declare a 2 dimensional array in js? (assuming its possible)

  2. How would I access its members? (myArray[0][1] or myArray[0,1] ? )

Thanks

+16  A: 
var items = [[1,2],[3,4],[5,6]];
alert(items[0][0]); // 1
Ballsacian1
A: 

Two dimensional arrays are created the same way single dimensional arrays are. And you access them like array[0][1].

var arr = [1, 2, [3, 4], 5];

alert (arr[2][1]); //alerts "4"
tj111
+6  A: 

You simply make each item within the array an array.

  var x = new Array(10);
  for (var i = 0; i < 10; i++) {
    x[i] = new Array(20);
  }
  x[5][12] = 3.0;
Sufian
Can they use things like strings for their keys and values?myArray['Book']['item1'] ?
Diego
@Diego, yes, but that's not what arrays are intended for. It's better to use an object when your keys are strings.
Matthew Crumley
+1  A: 

https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Working_with_Arrays#Two-Dimensional_Arrays

Milen A. Radev
+3  A: 

Javascript only has 1-dimensional arrays, but you can build arrays of arrays, as others pointed out.

The following function can be used to construct a 2-d array of fixed dimensions:

function Create2DArray(rows) {
  var arr = [];

  for (var i=0;i<rows;i++) {
     arr[i] = [];
  }

  return arr;
}

The number of columns is not really important, because it is not required to specify the size of an array before using it.

Then you can just call:

var arr = Create2DArray(100);

arr[50][2] = 5;
arr[70][5] = 7454;
// ...
Philippe Leybaert
A: 

You could allocate an array of rows, where each row is an array of the same length. Or you could allocate a one-dimensional array with rows*columns elements and define methods to map row/column coordinates to element indices.

Whichever implementation you pick, if you wrap it in an object you can define the accessor methods in a prototype to make the API easy to use.

Nat
+7  A: 

The reason some say that it isn't possible is because a two dimensional array is really just an array of arrays. The other comments here provide perfectly valid methods of creating two dimensional arrays in JavaScript, but the purest point of view would be that you have a one dimensional array of objects, each of those objects would be a one dimensional array consisting of two elements.

So, that's the cause of the conflicting view points.

James Conigliaro
+1  A: 

Similar to activa's answer, here's a function to create an n-dimensional array:

function createArray(length) {
    var a = new Array(length || 0);

    if (arguments.length > 1) {
        var args = Array.prototype.slice.call(arguments, 1);
        for (var i = 0; i < length; i++) {
            a[i] = createArray.apply(this, args);
        }
    }

    return a;
}

createArray();     // [] or new Array()

createArray(2);    // new Array(2)

createArray(3, 2); // [new Array(2),
                   //  new Array(2),
                   //  new Array(2)]
Matthew Crumley