I did:
a = Array(2).fill([]);
a[0].push(1);
Expected outcome: a = [[1], []]
Got: a = [[1], [1]]
Why does the second item depend on the first one here?
Thanks in advance.
From docs,
If the first parameter 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
Alternate using Array.from()
const a = Array.from({length: 2}, i => [])
a[0].push(1)
console.log(a)