When creating an array and populating it using the fill
method, I noticed that changing array1[0].foo
also changes all other objects in the array.
const array1 = Array(2).fill({ foo: null })
array1[0].foo = 'bar' // [ { foo: 'bar' }, { foo: 'bar' } ]
Is there a way to utilize the fill
method while ensuring each index contains a unique copy of the same object?