While working in p5js, I am trying to create a 2-dimensional boolean array in JavaScript. The following code snippet successfully creates a 3x3 array of booleans set to false:
var cells = Array.from({ length: 3 }, () =>
Array.from({ length: 3 }, () => false)
);
console.log(cells)
However, when I attempt to create this array as a member variable of a class, the values remain false regardless of any changes:
class ExampleClass{
constructor(){
this.cells = Array.from({ length: 3 }, () =>
Array.from({ length: 3 }, () => false)
);
this.cells[0][0] = true;
console.log(this.cells)
}
}
Even if I try to assign and manipulate the array outside of the class context, the values still default to false:
var cells = Array.from({ length: 3 }, () =>
Array.from({ length: 3 }, () => false)
);
cells[0][0] = true;
this.cells = cells
console.log(this.cells)
I suspect there might be a flaw in my understanding of how JavaScript initializes variables. How can I fix this issue and ensure that the array remains mutable even when declared as a member variable?