Skip to content

WebDevHubs

  • Home
  • HTML
  • CSS
  • JavaScript
  • Web Technologies
  • Web Templates
  • Toggle search form

How to Convert Set to an Array in JavaScript?

Posted on March 12, 2025March 12, 2025 By Jyoti No Comments on How to Convert Set to an Array in JavaScript?

The Set object is used to store unique values of any type. This article covers all approaches to convert set to an array in JavaScript.

1. Using Spread Operator (…)

The spread operator (...) is the most concise and modern way to convert a Set into an Array. It allows you to spread the elements of the Set into a new Array.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let mySet = new Set([1, 2, 3, 4]);
let arr = [...mySet];
console.log(arr); // Output: [1, 2, 3, 4]
let mySet = new Set([1, 2, 3, 4]); let arr = [...mySet]; console.log(arr); // Output: [1, 2, 3, 4]
let mySet = new Set([1, 2, 3, 4]);
let arr = [...mySet];

console.log(arr); // Output: [1, 2, 3, 4]

Note: Works in modern JavaScript (ES6 and later).

2. Using Array.from() Method

The Array.from() method creates array from array-like or iterable objects, including Sets.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let mySet = new Set([5, 6, 7, 8]);
let arr = Array.from(mySet);
console.log(arr); // Output: [5, 6, 7, 8]
let mySet = new Set([5, 6, 7, 8]); let arr = Array.from(mySet); console.log(arr); // Output: [5, 6, 7, 8]
let mySet = new Set([5, 6, 7, 8]);
let arr = Array.from(mySet);

console.log(arr); // Output: [5, 6, 7, 8]

Creating an array from set with mapping.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let mySet = new Set([2, 4, 6]);
let arr = Array.from(mySet, x => x * 2);
console.log(arr); // Output: [4, 8, 12]
let mySet = new Set([2, 4, 6]); let arr = Array.from(mySet, x => x * 2); console.log(arr); // Output: [4, 8, 12]
let mySet = new Set([2, 4, 6]);
let arr = Array.from(mySet, x => x * 2);

console.log(arr); // Output: [4, 8, 12]

3. Using Array map() With Spread Operator

You can use the spread operator in combination with map() method to convert a Set to an Array.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let mySet = new Set([1, 2, 3]);
let arr = [...mySet].map(x => x * 2);
console.log(arr); // Output: [2, 4, 6]
let mySet = new Set([1, 2, 3]); let arr = [...mySet].map(x => x * 2); console.log(arr); // Output: [2, 4, 6]
let mySet = new Set([1, 2, 3]);
let arr = [...mySet].map(x => x * 2);

console.log(arr); // Output: [2, 4, 6]

4. Using for…of Loop

A for...of loop allows manual iteration over the Set to populate an array.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let mySet = new Set([10, 20, 30]);
let arr = [];
for (let item of mySet) {
arr.push(item);
}
console.log(arr); // Output: [10, 20, 30]
let mySet = new Set([10, 20, 30]); let arr = []; for (let item of mySet) { arr.push(item); } console.log(arr); // Output: [10, 20, 30]
let mySet = new Set([10, 20, 30]);
let arr = [];

for (let item of mySet) {
    arr.push(item);
}

console.log(arr); // Output: [10, 20, 30]

5. Using forEach() Method

The forEach() method allows you to iterate over each element of the Set and manually build an array.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let mySet = new Set([40, 50, 60]);
let arr = [];
mySet.forEach(item => {
arr.push(item);
});
console.log(arr); // Output: [40, 50, 60]
let mySet = new Set([40, 50, 60]); let arr = []; mySet.forEach(item => { arr.push(item); }); console.log(arr); // Output: [40, 50, 60]
let mySet = new Set([40, 50, 60]);
let arr = [];

mySet.forEach(item => {
    arr.push(item);
});

console.log(arr); // Output: [40, 50, 60]

6. Using Lodash’s _.toArray() Method

The Lodash _.toArray() method easily convert a Set into an Array.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const _ = require('lodash');
let mySet = new Set([70, 80, 90]);
let arr = _.toArray(mySet);
console.log(arr); // Output: [70, 80, 90]
const _ = require('lodash'); let mySet = new Set([70, 80, 90]); let arr = _.toArray(mySet); console.log(arr); // Output: [70, 80, 90]
const _ = require('lodash');

let mySet = new Set([70, 80, 90]);
let arr = _.toArray(mySet);

console.log(arr); // Output: [70, 80, 90]

7. Using Destructuring With Assignment

You can use destructuring to spread the elements of a Set directly into an array.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let mySet = new Set([100, 200, 300]);
let arr = [...mySet];
console.log(arr); // Output: [100, 200, 300]
let mySet = new Set([100, 200, 300]); let arr = [...mySet]; console.log(arr); // Output: [100, 200, 300]
let mySet = new Set([100, 200, 300]);
let arr = [...mySet];

console.log(arr); // Output: [100, 200, 300]
JavaScript, Web Technologies Tags:JavaScript-Array, JavaScript-Questions, JavaScript-Set

Post navigation

Previous Post: How to Create Two-Dimensional Array in JavaScript?
Next Post: How to Compare Arrays in JavaScript?

Leave a Reply Cancel reply

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

Archives

  • July 2025
  • June 2025
  • May 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024

Categories

  • CSS
  • HTML
  • Interview Experience
  • Java
  • JavaScript
  • Lodash
  • PHP
  • Programs
  • Python
  • Selenium
  • Software Testing
  • Web Technologies
  • Web Templates

Recent Posts

  • Selenium – A Web Application Testing Framework
  • Java Program to Check Whether a Given String is Palindrome or Not
  • Java Program to Find the Second Largest Element of an Array
  • Adobe Interview Experience | Senior QA Engineer | Automation Test for Noida Office [Latest 2025]
  • Java Program to Find the Largest Element of an Array

Recent Comments

No comments to show.

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme