2

I am trying to change the values of an array created like this:

let newArr = Array(8).fill().map(() => Array(8).fill({ unit: null, highlighted: false, color: null }));

That is an array inside which is an array inside which is an object. Whenever I try to change the values of the object, all the values corresponding to the first array outside the object are changed. If I write:

newArr[0][4].unit = "King";

Then all the unit properties of newArr[0] are changed to "King" (newArr[0][0].unit = "King", newArr[0][1].unit = "King", newArr[0][2].unit = "King".. and so on.) while I just want newArr[0][4].unit to be equal to "King". I think the problem is in the way I am defining the array but don't know how to fix that. I searched a lot on the internet but couldn't find a fix.

hjjinx
  • 55
  • 6

0 Answers0