Image by Matthew Henry from Unsplash
DESCRIPTION
Clones might take over the world, we - as JavaScript guardians need to learn how to remove duplicates from an array.
Preview
removeDuplicates one-liner
JavaScript
Summary
- We created
removeDuplicates
function which accepts anarray
argument. - The above code needs to be read from the right. We're transforming our duplicates
array
into anew Set
. Set is a specific data structure that accepts only unique values. - After that, we're getting back to an array, using the Rest spread operator with array syntax,
[...]
. - One-liner, clean and modern, one more time: duplicates array -> new Set(array) = only unique values -> [...] spread into an array again = no more clones!