I would like to create a two-dimensional array that gets initialized with booleans which are set to false. Currently I'm using this method of array creation:
const rows = 3
const cols = 5
const nestedArray = new Array(rows).fill(
new Array(cols).fill(false)
)
The nestedArray
looks fine, but as soon as I change the value of nestedArray[0][2]
, the values of nestedArray[1][2]
and nestedArray[2][2]
also get changed.
I guess this is because the sub-arrays are identical, probably because they get filled into the parent array by reference and not by value.
What would be an elegant and efficient way of creating an array of non-identical sub-arrays instead?