I'm trying to "paint" some grid with a rectangle area. But when I try to modify just one row the array modifies all of them. I tried to initialize the array in several ways, but the problem seems to be when I try to modify it, not the Array object.
var multiArray = Array(8).fill(Array(8).fill(false));
// Fill square 3x1 at (2, 4)
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 1; j++) {
multiArray[2 + i][4 + j] = true;
}
}
document.getElementById("content").innerHTML =
multiArray.map(a => a.map(e => e ? 'X' : '0').join(''))
.join('<br />');
<div id="content" />
At this point instead of providing a solution for this case I only want to understand why is that happening. What's the problem with [][] expression.
I'm trying this in Chrome version 51.0