How to Remove Array Duplicates in ES6

Samantha Ming
DailyJS
Published in
4 min readJan 14, 2019

--

Code Tidbit by SamanthaMing.com

Here are three ways to filter out duplicates from an array and return only the unique values. My favorite is using Set cause it’s the shortest and simplest 😁

1. Using Set

Let me start first by explaining what Set is:

Set is a new data object introduced in ES6. Because Set only lets you store unique values. When you pass in an array, it will remove any duplicate values.

Okay, let’s go back to our code and break down what’s happening. There are 2 things going on:

  1. First, we are creating a new Setby passing an array. Because Setonly allows unique values, all duplicates will be removed.
  2. Now the duplicates are gone, we’re going to convert it back to an array by using the spread operator ...

Convert Set to an Array using Array.from

Alternatively, you can also use Array.from to convert a Set into an array:

--

--

Samantha Ming
DailyJS

Frontend Developer sharing weekly JS, HTML, CSS code tidbits🔥 Discover them all on samanthaming.com 💛