Skip to content

Edvins Antonovs

How to remove duplicates from array in JavaScript

There are multiple ways how to remove a duplicate value from a JavaScript array using indexOf() and filter(), and forEach() and include() methods. Yet using Set with a combination of spread operator is the simplest approach.

A Set is an iterable object which lets you store unique values of any type, whether primitive values or object references. We use the spread syntax (...) to convert the Set object to an array.

1const arr = [1, 2, 3, 5, 3, 8, 13, 13, 21, 5, 3, 2, 21];
2
3console.log([...new Set(arr)]);
4// Output: [1, 2, 3, 5, 8, 13, 21]
© 2024 by Edvins Antonovs. All rights reserved.