I hit a weird behavior when trying to generate and use symetrically generated 2D Matrices in Node/Chrome. The generating method is shown below.
const test=(f) => {
// Omitted
// Could provide full source if needed but it's not small
}
const gen=(boxSize)=>{
const boardSize = boxSize ** 2;
return new Array(boardSize).fill(new Array(boardSize).fill(0));
}
console.log(gen(2)); // [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
test(gen(2)); // fails
test(JSON.parse(JSON.stringify(gen(2)))); // passes
test([[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]); // passes
Why do I get this inconsistent(?!) behavior between they ways of initializing the arrays?