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

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Miscellaneous / Remove Duplicates From an Array

Remove Duplicates From an Array

Remove Duplicates From an Array

In JavaScript, Set is a collection that lets you store only unique values. This means any duplicated values are removed.

So, to remove duplicates from an array, you can convert it to a set, and then back to an array.

const numbers = [1, 1, 20, 3, 3, 3, 9, 9];
const uniqueNumbers = [...new Set(numbers)]; // -> [1, 20, 3, 9]

Here’s how it works. new Set(numbers) creates a set from a list of numbers. Creating a set automatically removes all duplicate values. The spread operator ... converts any iterable to an array. This means [...new Set(numbers)] converts the set right back to an array.

Source

https://medium.com/geekculture/20-javascript-snippets-to-code-like-a-pro-86f5fda5598e

Miscellaneous

Related Snippets:

  • Determine if a string contains a substring
  • How to Check if an Array Includes a Value in JavaScript
  • Show or hide an element
  • Capitalize the first letter of every word in a string

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2023 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us