Playing around with different ways of instantiating Arrays with Javascript and I noticed some interesting behavior:
matrix = Array(3).fill(Array(3).fill(0))
Creates an NxN matrix of 0 values
[
[0,0,0],
[0,0,0],
[0,0,0]
]
I then tried changing the first row of the matrix to be all 1's:
matrix[0].fill(1)
Which for some reason turned ALL values in the matrix to 1's:
[
[1,1,1],
[1,1,1],
[1,1,1]
]
This behavior doesn't make sense to me. Shouldn't only the first row be affected by the final call to Array.fill? What's going on here?