Removing Array Duplicates In ES6
Here are 4 ways to remove duplicates from an array and return only the unique values.
Set is one of the cool thing happening in ES6. It’s a new data structure that stores unique values of any type. You can iterate through the elements of a set in insertion order. A value in the Set may only occur once; it is unique in the Set’s collection.
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
- Bonus: Retrieve the duplicate values
In order to find out this option, let’s understand what these two methods are doing: reduce and includes.
The reduce() method executes a reducer function (that you provide) on each member of the array resulting in single output value.
The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
Do not forget: The bundle size for firstname.lastname@example.org is 24.2kB (minified + gzipped). See here.