FANDOM


Something that JavaScript appears to be lacking that is often available in other programming languages is a multidimensional Array object. In fact, though, you can get the same functionality by using Arrays full of Arrays!

There's a little bit of extra legwork involved - namely, you have to make sure you create all your Array objects before you try and access them. The Create2DArray() function below can be used to initialize a 2-dimensional array.

function Create2DArray(rows, columns)
{
  var arr = new Array(rows);
  
  for (var i = 0; i < rows; i++) {
    arr[i] = new Array(columns);
  }
  
  return arr;
}

my2DArray = Create2DArray(4, 15);

for (var i = 0; i < 4; i++) {
  for (var j = 0; j < 15; j++) {
    my2DArray[i][j] = i * j;
  }
}

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.