Front-end education for the real world. Since 2018.





Use a set to remove array duplicates

Andy Bell

Topic: JavaScript

If you have an array of items that contains duplicates, you can remove them all by spreading the array into a new Set.

Code language
javascript

const array = ['Hello', 'Hi', 'Hello', 'Ciao'];

const filtered = [...new Set(array)];

console.log(filtered); // ['Hello', 'Hi', 'Ciao']

Enjoyed this article? You can support us by leaving a tip via Open Collective


Newsletter