0

I declare a 2-dimensional array

var arr = new Array(4).fill([])
// arr = [[],[],[],[]]

When I want to push a value to one of its elements, it cause others do the same

arr[0].push(1)
// arr = [[1],[1],[1],[1]]

Why that happend, how to fix it?

  • 1
    [_"`value`: Value to fill the array with. Note all elements in the array will be this exact value: if value is an object, each slot in the array will reference that object."_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill#parameters) – Andy Dec 23 '22 at 10:44

0 Answers0