I generate a 3x3 grid
const grid = Array(3).fill(Array(3).fill(0))
[[0,0,0],
[0,0,0],
[0,0,0]]
I set the value of the middle cell to 1
grid[1][1] = 1
I expect to see
[[0,0,0],
[0,1,0],
[0,0,0]]
but instead see
[[0,1,0]
[0,1,0],
[0,1,0]]
It seems that all rows are referring to the same array in memory.
A console.log(grid)
shows
[[0, 1, 0],
[circular object Array],
[circular object Array]]
What exactly is going on? How can I properly generate a matrix where each cell is independent?