• 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 a Set to Array in JavaScript

Convert a Set to Array in JavaScript

August 9, 2022 Leave a Comment

To convert a set to array in JavaScript we simply need to pass the set as a parameter in the JavaScript Array.from method.

var numbers_set = new Set([11, 48, 101, 15, 41, 3, 13, 92]);
var numbers_array = Array.from(numbers_set);

console.log(numbers_array);

#Output:
[11, 48, 101, 15, 41, 3, 13, 92]

Let’s take a look at another example of this below.


Let’s say we have a simple set of colors and we want to convert the set to an array. To do this we simply use the Array.from() method again, passing it the set of colors.


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

console.log(arrayOfColors);

In this example, the resulting array, arrayOfColors, would now contain the following:

[‘red’, ‘blue’, ‘green’, ‘yellow’, ‘orange’, ‘black’, ‘purple’, ‘pink’]

Converting a Set to Array Using the JavaScript forEach() Method

We can also convert a set to an array by using the JavaScript forEach() method. The forEach() method will simply go through all of the items in the set, which we will then add to a new array using the push() method.


var someSet = new Set(["somestring",true,{name: "Tom", age: "47"}, 457, "This is a string of text"]);

var setToArray = [];

someSet.forEach(function (eachItem) {
  setToArray.push(eachItem);
});

console.log(setToArray);

#Output
['somestring', true, {name: 'Tom', age: '47'}, 457, 'This is a string of text']

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

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Generate a Random Float Between 0 and 1
  • 2.  Using JavaScript to Set the Id of an Element
  • 3.  Using JavaScript to Get the Decimal Part of a Number
  • 4.  Using JavaScript to Get New Date Format in dd mm yy
  • 5.  JavaScript value – Get the Value from an Input Field
  • 6.  Examples Using the JavaScript += Addition Assignment Operator
  • 7.  Using JavaScript to get the Last Element in Array
  • 8.  How to Use JavaScript to Check if a String is Empty
  • 9.  Set Hidden Field Value In JavaScript
  • 10.  How to Swap Values in an Array in JavaScript

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