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);
0

5 Answers 5

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. ​

Sign up to request clarification or add additional context in comments.

3 Comments

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?
@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"
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
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);

2 Comments

What's wrong with my answer?
Works for my purposes: the tic-tac-toe React tutorial. A contention could be that you're restricted to the same number of indices for each sub array.
2

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

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

3 Comments

thank you Slaks, and how do i get the length of each dimension?
There is no length of each dimension. It's just an array of arrays; each array has its own length.
"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 :) ). developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/… (BTW I think your answer doesn't really answer the question. ;) )
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]]

1 Comment

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); }
-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

1 Comment

err...no this won't work. What you have here is 5 distinct arrays that are in no way related.