21

I would like to know how to declare 2 dimensional arrays in java script of fixed lengths something like,

var array = new Array[this.rows, this.columns];

after that I would like to get the length of each dimension like,

array[0].length; // I am assuming this would give me the first dimension like in C# array.GetLength(0);
array[1].length; // I am assuming this would give me the second dimension like in C# array.GetLength(1);
peterh
  • 11,875
  • 18
  • 85
  • 108

5 Answers5

36

A quick example of what @SLaks is referring to with jagged arrays. You basically put an array in an array. The below example shows one way to make an array thats 100x100.

var arr = [];
for(var x = 0; x < 100; x++){
    arr[x] = [];    
    for(var y = 0; y < 100; y++){ 
        arr[x][y] = x*y;    
    }    
}

console.log(arr[10][11]);

Live Demo

This method is very flexible for instance arr[4] could have an array indexed to 10, and arr[5] could have an array with 1 value, or be even be a completely different type such as a string or number. ​

Loktar
  • 34,764
  • 7
  • 90
  • 104
  • Hi Loktar thank you I like the demo and how do i get the dimensions of the array? the first dimension and the second dimension? –  Mar 15 '12 at 17:14
  • 1
    @Kathy Assuming the array lengths are equal in number (think of a checker board 8x8) you can do `arr.length`, and `arr[0].length`. That would be the "width" and "height" – Loktar Mar 15 '12 at 17:16
  • In case anyone still looking for an answer 1 liner code `var array = Array(y).fill().map(entry => Array(x))` `//x = number of column` `//y= number of row` – Kevin Ng Mar 28 '22 at 07:54
5

A nested 3x3 array (of undefined):

var arr = new Array(3);
for (var i = 0; i < arr.length; ++i) {
  arr[i] = new Array(3);
}

console.log(arr);

modle13 commented that you're restricted to a fixed size sub-array, so here's one quick tweak to get around that:

var nestedSizes = [3,5,1,4];
var arr = new Array(nestedSizes.length);
for (var i = 0; i < arr.length; ++i) {
  arr[i] = new Array(nestedSizes[i]);
}

console.log(arr);
c24w
  • 7,421
  • 7
  • 39
  • 47
2

Unlike C#, Javascript does not support multi-dimensional arrays.

Instead, you can use nested ("jagged") arrays.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • thank you Slaks, and how do i get the length of each dimension? –  Mar 15 '12 at 17:10
  • 2
    There is no length of each dimension. It's just an array of arrays; each array has its own length. – SLaks Mar 15 '12 at 17:13
  • 1
    _"Javascript does not support multi-dimensional arrays"_ - But array of arrays provide the equivalent of multi-dimensional arrays, so you can say JavaScript DOES support multi-dimensional arrays (even if they are not _truly_ multi-dimensional, I think your first sentence is not _truly_ correct in this form :) ). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Predefined_Core_Objects#Multi-Dimensional_Arrays (BTW I think your answer doesn't really answer the question. ;) ) – Sk8erPeter Feb 12 '14 at 13:40
2

In javascript:

var Matrix = function (rows, columns)  {
    this.rows = rows;
    this.columns = columns;
    this.myarray = new Array(this.rows);
    for (var i=0; i < this.columns; i +=1) {
        this.myarray[i]=new Array(this.rows)
    }
    return this.myarray;
}

var m = new Matrix(2,2);
m; // [[undefined, undefined], [undefined, undefined]];

var m2 = new Matrix(3,3);
m2; // [[undefined, undefined, undefined], [undefined, undefined, undefined], [undefined, undefined, undefined]]
antonjs
  • 14,060
  • 14
  • 65
  • 91
  • Works only when rows and columns are equal. Change the for loop to for (var i=0; i < this.rows; i+=1) { this.myarray[i]=new Array(this.columns); } – prit4fun Mar 17 '18 at 10:35
-4

could you not just do something like this?

var x = new Array(4);             #1st array, array of arrays                              
y = new Array(1, 4, 2, 0);       #2nd dimension of arrays, an array of values                  
z = new Array(4, 8, 3, 9);       #^^^                       
a = new Array(7, 0, 2, 4);       #^^^                  
t = new Array(9, 0, 3, 1);        #^^^

then to access 7 (the 1st value) in array a (3) you could type:

var example = x.3.1

if this wont work please tell me cos this is what i was told and what im now using to program my game