How to remove duplicates from an array with vanilla JS

Today, we’re going to learn how to remove duplicates from an array in JavaScript.
Let’s imagine you had an array of sandwiches, and some of the items in it were listed more than once.
let sandwiches = [‘turkey’, ‘ham’, ‘turkey’, ‘tuna’, ‘pb&j’, ‘ham’, ‘turkey’, ‘tuna’]; You want to get an updated list with the duplicates removed, a process called deduplication.
This used to require pairing the Array.filter() method with Array.


This content originally appeared on Go Make Things and was authored by Go Make Things

Today, we’re going to learn how to remove duplicates from an array in JavaScript.

Let’s imagine you had an array of sandwiches, and some of the items in it were listed more than once.

let sandwiches = ['turkey', 'ham', 'turkey', 'tuna', 'pb&j', 'ham', 'turkey', 'tuna'];

You want to get an updated list with the duplicates removed, a process called deduplication.

This used to require pairing the Array.filter() method with Array.indexOf(). And while that method still works, today, it’s a bit simpler to use Array.from() with the Set() object.

The new Set() constructor creates an iterable collection of items, just like an array. But, each item can only be included once.

To remove any duplicates, we can pass our array into the new Set() constructor, then convert the returned collection back into an array.

let deduped = Array.from(new Set(sandwiches));

Here’s a demo.

If you’d prefer, you can use the spread syntax instead of Array.from().

let deduped = [...new Set(sandwiches)];

I’ve put together a helper function you can use if you’d like.


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2021-04-01T14:30:00+00:00) How to remove duplicates from an array with vanilla JS. Retrieved from https://www.scien.cx/2021/04/01/how-to-remove-duplicates-from-an-array-with-vanilla-js/

MLA
" » How to remove duplicates from an array with vanilla JS." Go Make Things | Sciencx - Thursday April 1, 2021, https://www.scien.cx/2021/04/01/how-to-remove-duplicates-from-an-array-with-vanilla-js/
HARVARD
Go Make Things | Sciencx Thursday April 1, 2021 » How to remove duplicates from an array with vanilla JS., viewed ,<https://www.scien.cx/2021/04/01/how-to-remove-duplicates-from-an-array-with-vanilla-js/>
VANCOUVER
Go Make Things | Sciencx - » How to remove duplicates from an array with vanilla JS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/01/how-to-remove-duplicates-from-an-array-with-vanilla-js/
CHICAGO
" » How to remove duplicates from an array with vanilla JS." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2021/04/01/how-to-remove-duplicates-from-an-array-with-vanilla-js/
IEEE
" » How to remove duplicates from an array with vanilla JS." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2021/04/01/how-to-remove-duplicates-from-an-array-with-vanilla-js/. [Accessed: ]
rf:citation
» How to remove duplicates from an array with vanilla JS | Go Make Things | Sciencx | https://www.scien.cx/2021/04/01/how-to-remove-duplicates-from-an-array-with-vanilla-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.