Which fix removes the bug?
const users = [{ id: 1 }, { id: 1 }];
const unique = [...new Set(users)];
console.log(unique.length); // expected 1
- Spread users into a new array
- Use a
WeakSetinstead here - Sort users before the Set
- Key a Map by each
user.id
A Set compares objects by reference, and the two literals are different objects.
Play Problem Solving ▸