hanki.dev

Don't use Array.fill with objects!

If you create an array populated with objects using fill({}), it will actually just create 1 instace of the object.

const arr = new Array(3).fill({});
console.log(arr); // Output: [ {}, {}, {} ]
arr[0].key = '🤙';
console.log(arr); // Output: [ { key: '🤙' }, { key: '🤙' }, { key: '🤙' } ]

As you can see, that's absolutely not the behaviour you'd expect. Here's a simple fix to the problem:

const arr = new Array(2).fill().map(u => ({}));

#javascript