I am trying to set a single value in a two dimensional array, but its not working.
Consider the following code taken from the many examples on the subject:
// create a 3x3 array with -1 in every position:
let a = new Array(3).fill(new Array(3).fill(-1))
console.log(`a: ${JSON.stringify(a)}`)
a[1][2] = 0
console.log(`a: ${JSON.stringify(a)}`)
The output is as follows:
a: [[-1,-1,-1],[-1,-1,-1],[-1,-1,-1]]
a: [[-1,-1,0],[-1,-1,0],[-1,-1,0]]
As we can see, instead of setting a single cell, it as actually set 3 positions in the array to 0, i.e. it sets [0][2] and [1][2] and [2][2] = 0. Very odd.
I tried this:
let a = new Array(3).fill(new Array(3).fill(-1))
console.log(`a: ${JSON.stringify(a)}`)
a[1,2] = 0
console.log(`a: ${JSON.stringify(a)}`)
Which gives the even stranger result:
a: [[-1,-1,-1],[-1,-1,-1],[-1,-1,-1]]
a: [[-1,-1,-1],[-1,-1,-1],0]
Am I going crazy, or does javascript not support setting a value in a 2 dimensional array?