var a = Array(100).fill(Array(100).fill(false));
a
contains an array, each element of which references to an array. you are filling the outer array with an array which contains all false values. The inner array is being made only once and reference to the same array is passed to each element of outer array that is why if you perform an operation on one element it reflects on other elements as well.
This is actually equivalent to
var a1 = Array(100).fill(false);
var a = Array(100).fill(a1);
here a
gets 100 elements all having reference to same array a1
. So if you change one element of a
, all elements change since they are references to same array.
you will need to fill each element in outer array with a new array. you can do something like this:
var a = [];
for(var i=0; i<100; i++)
a.push(Array(100).fill(false));
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…