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

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Create a Unique Array

Using JavaScript to Create a Unique Array

September 16, 2022 Leave a Comment

We can use JavaScript to create a unique Array by converting the array to a set with the new Set() method and then back to an array with the Array.from() method.

var numbersArray = [0,7,7,7,0,2,3,1,1,4,5,6,7];

var convertToSet = new Set(numbersArray);

var convertBackToArray = Array.from(convertToSet);

console.log(convertBackToArray);

#Output:
[0, 7, 2, 3, 1, 4, 5, 6]

Look below to see a more compact version of this code.


When working with collections of data in JavaScript, the ability to get the unique values of your data is valuable.

To get the unique values of an array in JavaScript, the easiest way is by converting the array to a set with new Set() method and then back to an array with Array.from() method.

A set is an unordered collection of unique elements. On the other hand, arrays can contain duplicates.

Converting an array to a set creates a set with the same items as the array and returns a unique array.

Below is our example again of how to create a unique array with the new Set() method in JavaScript.

var numbersArray = [0,7,7,7,0,2,3,1,1,4,5,6,7];

var convertToSet = new Set(numbersArray);

var convertBackToArray = Array.from(convertToSet);

console.log(convertBackToArray);

#Output:
[0, 7, 2, 3, 1, 4, 5, 6]

We can put our code in a function, uniqueArray() to make it easy to reuse this code.

function uniqueArray(arr){
  return Array.from(new Set(arr));
};

And finally, let’s see this function in action using our same example.

function uniqueArray(arr){
  return Array.from(new Set(arr));
};

var numbersArray = [0,7,7,7,0,2,3,1,1,4,5,6,7];

console.log(uniqueArray(numbersArray));

#Output:
[0, 7, 2, 3, 1, 4, 5, 6]

Hopefully this article has been useful for you to learn how to use JavaScript to create a unique array.

Other Articles You'll Also Like:

  • 1.  Remove Commas From a String in JavaScript
  • 2.  Using JavaScript to Get the URL Path
  • 3.  Finding the Length of a String in JavaScript
  • 4.  JavaScript onfocusout – How to Use the onfocusout Event on an HTML Form
  • 5.  JavaScript sin – Find Sine of Number in Radians Using Math.sin()
  • 6.  How to Change the Id of an Element in JavaScript
  • 7.  Using JavaScript to Create an Empty Array
  • 8.  Using JavaScript to Reverse an Array
  • 9.  How to Remove All Numbers From String in JavaScript
  • 10.  Using JavaScript to Check a Radio Button

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy