Removing duplicate elements in Javascript: In a simple way! 😡

Maria Antonella πŸ¦‹ - Aug 26 '21 - - Dev Community

The last few weeks I have been learning Python. I'm following this roadmap: 30-days-of-python Series' Articles by @arindam Dawn. I recommend it ! I love it 🀩

Anyway, when I was learning Python I saw that the SET framework existed, and I was fascinated.
But thanks to that I discovered that it also existed in Javascript, and I didn't know it!

With SET we can remove duplicate elements from an array in a very easy way.

JavaScript's built-in Set object is described as a collection of values, where each value may occur only once. A Set object is also iterable, making it easily convertible to an array using the spread (...) operator.

🍭 Here are some examples of this:

const nums = [1, 2, 2, 3, 1, 2, 4, 5, 5, 6, 4, 2, 6];
[...new Set(nums)] // [1, 2, 3, 4, 5, 6]
Enter fullscreen mode Exit fullscreen mode
const something = ['anto', 'anto', 'antonella', 'ant', 'ant', 2, 4, 5, 6,2,2];
[...new Set(something)] // [ 'anto', 'antonella', 'ant', 2, 4, 5, 6 ]
Enter fullscreen mode Exit fullscreen mode
const emojisFruits = ['πŸ‹','πŸ‹', 'πŸ‡ ','πŸ‰' ,'πŸ‡ ','πŸ“' ,'πŸ’','πŸ’','🍈 ','πŸ’'];
[...new Set(emojisFruits)] // [ 'πŸ‹', 'πŸ‡ ', 'πŸ‰', 'πŸ“', 'πŸ’', '🍈 ' ]
Enter fullscreen mode Exit fullscreen mode

Very useful!!!
Did you also know about this?

. . . . . . . . . . . . . . . . . .
Terabox Video Player