๐จ๐๐ฒ ๐ฆ๐ฒ๐ ๐ณ๐ผ๐ฟ ๐จ๐ป๐ถ๐พ๐๐ฒ ๐๐ฎ๐๐ฎ
You work with lists in JavaScript. User IDs. Emails. Search results. These lists often have duplicates.
Use a Set object. It stores unique values. It removes duplicates automatically.
Example: const numbers = [1, 2, 2, 3, 4, 4]; const cleanNumbers = [...new Set(numbers)]; // Result: [1, 2, 3, 4]
This pattern cleans data fast. Skip loops. Skip manual checks.
Use it for:
- Email lists from APIs.
- UI filters.
- User permissions.
- Tag systems.
Check if a value exists with .has(). const tags = new Set(); tags.add("js"); tags.has("js"); // true
Stick to arrays if you need order or indexes. Use Set for uniqueness. Your code stays clean.