I'm trying to create a 6 by 12 Matrix using Array.fill
let m = Array(6).fill(Array(12).fill(0));
While this works, the problem is that the inner Arrays are actually all referencing the same Array
object.
let m = Array(6).fill(Array(12).fill(0));
m[0][0] = 1;
console.log(m[1][0]); // Outputs 1 instead of 0
I wanted (and expected) the value of m[1][0]
to be 0
.
How can I force Array.fill
fill copy-by-values of the given argument (eg: Array(12).fill(0)
is the argument in my case) instead of copying by reference ?