Removing Array Duplicates in ES6

January 19, 2019

There are many ways to remove duplicates from an array in Javascript, but ES6 specifically offers some interesting and functional methods for doing so.

const array = ["my", "array", "my", "array", "elements", "array"];

// Using filter
array.filter((item, index) => array.indexOf(item) === index);

// Using reduce
array.reduce((accum, item) => accum.includes(item) ? accum : [...accum, item], []);

// Using Set
[...new Set(array)];