• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

How to Remove Duplicate Items From an Array in JavaScript (the Easy Way)

Javascript · October 14, 2021

Languages such as PHP have methods for de-duplicating arrays, but Javascript strangely does not. In PHP, you can write. array_uniqueAnd this function will remove duplicates.

There are so many solutions online you will find for this problem. They mostly centre around using filter and index checks, others recommend libraries like Lodash.

Forget installing libraries or writing convoluted functional programming inspired solutions. You can do this:

const arr = [1, 2, 3, 9, 1, 6, 4, 3, 9, 2, 4, 6];
const unique = [...new Set(arr)];

If you were to console log the contents of unique using the spread operator, we convert it back to a unique array. You would get an array containing: [1, 2, 3, 9, 6, 4] — the Set object is creating for storing unique collections, not only arrays but other types of data too.

Dwayne

Leave a Reply Cancel reply

0 Comments
Inline Feedbacks
View all comments

Primary Sidebar

Popular

  • How To Get The Hash of A File In Node.js
  • Testing Event Listeners In Jest (Without Using A Library)
  • Which Neural DSP Archetype Plugins Should You Buy?
  • NBN Box Installed Inside of Garage, Where Do You Put The Modem?
  • Smoke Detector Randomly Goes Off Early Hours of The Morning
  • How to Use Neural DSP Archetype Plugins With the Quad Cortex
  • How to Fast Launch Microsoft Flight Simulator 2020 (decrease game loading time)
  • Wild Natural Deodorant Review
  • How To Install Eufy Security Cameras Without Drilling or Using Screws
  • Perfectly Smoked Steak On A Charcoal BBQ Using Indirect Heat

Recent Comments

  • Thebe on How to Remove the My Sites Menu From the WordPress Admin Bar
  • Maccas worker jn the 2000s on Dear McDonald’s: bring back the Warm Cookie Sundae, you cowards
  • Anamika Singh on Testing Event Listeners In Jest (Without Using A Library)
  • Stefan on A List of WordPress Gutenberg Core Blocks
  • pandammonium on A List of WordPress Gutenberg Core Blocks

Copyright © 2022 · Dwayne Charrington · Log in

wpDiscuz