1

I created an array using Array and fill however after that I cannot edit a single value of this.

 const board = new Array(4).fill(Array(4).fill(0))


 board[0][0] = 1

This changes first index of every nested array.

[1, 0, 0, 0]
[1, 0, 0, 0]
[1, 0, 0, 0]
[1, 0, 0, 0]

So it isn't correct to create multidimensional array this way?

KHypki
  • 11
  • 1
  • 2
    Because the `fill` function will re-use the passed array, so every position in the array will point to the same value in memory. – Ele Mar 10 '18 at 19:21

0 Answers0