Here is a quick and easy way to filter all unique values from an array using the Array.filter
and Array.indexOf
methods.
Explanation
The Array.filter
method returns a new array containing all elements that pass a predicate. With that in mind we can use the Array.indexOf
method, which returns the first index at which a given element can be found in the array, inside of the predicate in order to test whether or not the current index is the same as the first index of the element we are iterating on.
Since Array.filter
’s callback accepts three arguments (element
, index
, array
) we’ll use these values to make our predicate return true
only if the current element
’s index is the current index
.
Example
const sample = ['_conf.scss', '_mixin.scss', '_mixin.scss', '_mixin.scss', 'main.scss'];
// Return true when the indexOf(element) is the current index, otherwise we've seen element already.
const filtered = sample.filter((element, index, array) => array.indexOf(element) === index);
// => ['_conf.scss', '_mixin.scss', 'main.scss']