I have declared a two-dimensional array, like so:
a = [[]]
However, when I try to give a second dimension value using a first dimension index other than 0, it doesn’t work:
a[1][0] = "foo" //returns error
Is there a better way around this than manually defining every index you need as an array, i.e.:
a[1] = [];
a[2] = [];
a[3] = [];
//et cetera
N-Dimensional arrays do not exist in javascript – you have to just make arrays containing arrays as elements.
You’re getting an error because
a = [[]];declares an array with one element, which happens to also be an array. Thereforea[0]is the internal array, buta[1]does not exist because you never declared it. The easiest way to properly declare a “two dimensional array” would be to use a loop: