I would like to create a 2d array, but i found an interesting behavior
const column = 10;
const row = 10;
let matrix = new Array(row).fill(new Array(column).fill(0));
matrix[0][1] = 1;
console.log(matrix)
and to my surprise i get the result as below:
0 2 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0 0
the entire column number 1 is set to 1, may I know why am I getting this behavior?
new Array(column).fill(0)creates an array with lengthcolumnand elements as0.new Array(row).fill(....)which fills with the array you were created, which is not creating different array for each... instead the reference to the array act as element