ES6 introduced Map and Set to solve limitations with plain Objects and Arrays.
Set (Unique Values)
The fastest way to remove duplicates.
javascript
const emails = ["a@a.com", "b@b.com", "a@a.com"];
const uniqueEmails = [...new Set(emails)];
console.log(uniqueEmails); // ["a@a.com", "b@b.com"]Map (Better Key-Value)
Why Map instead of Object?
- Keys can be ANY type (Objects, Functions), not just Strings.
- Preserves insertion order.
- Has a built-in
.sizeproperty (No need for Object.keys().length).
javascript
const cache = new Map();
const user = { id: 1 };
// Using an object as a key!
cache.set(user, "User Data");
console.log(cache.get(user)); // "User Data"