ES6 ES2015: Easily remove duplicates from an Array

Say you have a JavaScript Array containing duplicate values. By creating a Set – which only stores unique values (primitive values or object references) – and then spreading that Set into a new Array you can easily dedupe the given Array:

// Array with duplicates
const arr = [7, 3, 1, 3, 3, 7];

// Dedupe using a Set, which is then spread into a new Array
const deduped = [...new Set(arr)];

// -> [ 7, 3, 1 ]

I really dig the spread operator. There’s some more interesting stuff you can do with it.

Did this help you out? Like what you see?
Consider donating.

I don’t run ads on my blog nor do I do this for profit. A donation however would always put a smile on my face though. Thanks!

☕️ Buy me a Coffee ($3)

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.