我想创建一个二维数组,其中的布尔值都被初始化为 false。目前我正在使用以下的数组创建方法:
const rows = 3
const cols = 5
const nestedArray = new Array(rows).fill(
new Array(cols).fill(false)
)
nestedArray
看起来没问题,但只要我改变 nestedArray[0][2]
的值,nestedArray[1][2]
和 nestedArray[2][2]
的值也会被改变。
我想这是因为子数组是相同的,可能是因为它们被以引用的方式而不是以值的方式填入到父数组中。
有什么优雅和高效的方法来创建非相同的子数组的数组呢?