I need to create an array of arrays. Every nested array should be filled with zeroes. I'v tried this:
let test = Array
.from(
new Array(8)
.fill(
new Array(8).fill(0)
)
);
But then I'd got issue with a reference. For example if I do this:
test[1][2] = 1;
then every nested array has value 1
at index 2
:
console.log(test[2][2]) // 1
I came up with simple solution using a loop:
const testArray = new Array(8);
for (let i = 0; i < testArray.length; i++) {
testArray[i] = new Array(8).fill(0);
}
Now it works and there's no issue with reference. But I don't understand why this issue occurred in the first try. Can someone be so kind and explain that to me?