• 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 / Convert an Array to Set in JavaScript

Convert an Array to Set in JavaScript

June 7, 2022 Leave a Comment

To convert an array to a set in JavaScript we simply just need to pass the array as a parameter when creating a new set, new Set(). One of the main differences between an array and a set is that a set does not contain duplicate values.

var numbers_array = [13, 48, 92, 13, 48, 3, 13, 92];
var numbers_set = new Set(numbers_array);

console.log(numbers_set);

#Output:
{13, 48, 92, 3}

As you can see from the code above, the array, numbers_array, has multiple duplicate values in it. When we create a new set using the array as a parameter, the duplicate values in the array are left out of the set, and only the unique numbers are left.

Let’s take a look at another example below.


Let’s say we have a simple array of colors and we want to convert the array to a set. To do this we simply use the set constructor again, passing it the array of colors.


var arrayOfColors = ["red","blue","green","red","yellow","orange","black","purple","pink","blue","black"];
var color_set = new Set(arrayOfColors);

console.log(set);

In this example, the resulting set, color_set, would now contain the following:

{‘red’, ‘blue’, ‘green’, ‘yellow’, ‘orange’, ‘black’, ‘purple’, ‘pink’}

Hopefully this article has been useful to help you understand how to convert an array to set using JavaScript.

Other Articles You'll Also Like:

  • 1.  How to Get the First Character of a String in JavaScript
  • 2.  JavaScript offsetTop – Get the Top Position of Element
  • 3.  JavaScript value – Get the Value from an Input Field
  • 4.  Convert an Array of Values into a String Without Commas in JavaScript
  • 5.  JavaScript acos – Find Arccosine and Inverse Cosine of Number
  • 6.  Creating a JavaScript Function to Multiply Two Numbers
  • 7.  Using JavaScript to Add Trailing Zeros
  • 8.  Using JavaScript to Round to 4 Decimal Places
  • 9.  Remove Empty Strings from an Array in JavaScript
  • 10.  Using JavaScript to Get the Length of Array

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