up vote 26 down vote favorite
6
share [g+] share [fb]

I have been reading online and some places say it isn't 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 JavaScript? (assuming it's possible)

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

Thanks

link|improve this question

feedback

9 Answers

up vote 45 down vote accepted
var items = [[1,2],[3,4],[5,6]];
alert(items[0][0]); // 1
link|improve this answer
feedback

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;
link|improve this answer
Can they use things like strings for their keys and values? myArray['Book']['item1'] ? – Diego Jun 8 '09 at 19:54
3  
@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 Jun 8 '09 at 20:05
feedback

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.

link|improve this answer
3  
No, it's not. In some languages, you can have multidimensional arrays like string[3,5] = "foo";. It's a better approach for some scenarios, because the Y axis is not actually a child of the X axis. – Rafael Soares Aug 4 '11 at 15:29
feedback

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)]
link|improve this answer
Can this create a 4 dimensional array? – trusktr May 19 '11 at 2:18
@trusktr: Yes, you could create as many dimensions as you want (within your memory constraints). Just pass in the length of the four dimensions. For example, var array = createArray(2, 3, 4, 5);. – Matthew Crumley May 19 '11 at 4:21
Nice! I actually asked about this here: stackoverflow.com/questions/6053332/javascript-4d-arrays and a variety of interesting answers. – trusktr May 19 '11 at 5:50
feedback

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;
// ...
link|improve this answer
feedback

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"
link|improve this answer
feedback

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.

link|improve this answer
feedback

property type array in JavaScript

var model = {};

model.Action = [];

model.Caption = [];

model.Step = [];
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.