0
const foo = new Array(2) // [<empty>, <empty>]
            .fill(new Array(2)) // [[<empty>, <empty>], [<empty>, <empty>]]
            .map((subArr) => subArr.fill(0)); // [[0, 0], [0, 0]]

const result = foo.map((subArr, index) => {
  subArr.splice(index, 1, 1);
  return subArr; 
});

console.log(result) // [[1, 1], [1, 1]]

But the result is different in a similar case:

const foo = [[0, 0], [0, 0]];

const result = foo.map((subArr, index) => {
  subArr.splice(index, 1, 1);
  return subArr;
});

console.log(result); // [[1, 0], [0, 1]]

I do not understand what is the reason for such a difference, because in both cases I work with an array [[0, 0], [0, 0]]

I expect the same result in both cases

DivDisable
  • 11
  • 2

0 Answers0