I was watching a course on Udemy and I figure out a very cool way of removing eventual repetitions of elements of an array.
It is the combination of the class Set with the Spread Operator.
Set
Allows storage of unique values of any type.
Example of a set:
const set = new Set([1, 2, 3, 3, 4, 5, 5])
console.log(set) // Set {1, 2, 3, 4, 5}
Click here to know more about the class Set.
Spread operator
It will serve to spread the elements of the resulting Set.
Example of how the spread operator works:
const a = [1, 2, 3]
const b = [4, 5, 6]
const c = [...a, ...b]
console.log(c) // [1, 2, 3, 4, 5, 6]
Click here to learn more about the Spread operator.
Combining both
Now, we can combine both and remove the repetitions:
const initial = [1, 2, 2, 3, 4, 4, 8, 8]
const set = new Set(initial)
const final = [...set]
console.log(final) // [1, 2, 3, 4, 8]