How to convert Set to Array in JavaScript ?
To convert a Set into an Array, we need to know the main characteristics of a set. A set is a collection of unique items, i.e. no element can be repeated. Set in ES6 are ordered i.e. elements of the set can be iterated in the insertion order.
convert Set to Array in JavaScript
A set can be converted to an array in JavaScript in the following ways:
This method returns a new Array from an array, object or other iterable objects like Map, Set, etc. It takes the set as parameter and converts it to an array.
Syntax:
const arrayName = Array.from(setName);
Example: This example converts a Set into an Array using the Array.from() method
Javascript
const mySet = new Set([1, 1, 2, 3, 4, 4, 5, 6, 5]);
console.log( "Elements in the set are: " , mySet);
let myArr = Array.from(mySet);
console.log( "Elements in the Array created using Set are: " , myArr);
|
Output
Elements in the set are: Set(6) { 1, 2, 3, 4, 5, 6 }
Elements in the Array created using Set are: [ 1, 2, 3, 4, 5, 6 ]
The spread operator can be used to destructure the elements of the array and then assign them to the new variable in the form of an array.
Syntax:
const arrayName = [...setName];
Example: The below code converts a set into an array using the spread operator.
Javascript
let mySet = new Set([ 'GFG' , 'JS' ]);
console.log( "Elements in the set are: " , mySet);
let myArr = [...mySet];
console.log( "Elements in the Array created using Set are: " , myArr);
|
Output
Elements in the set are: Set(2) { 'GFG', 'JS' }
Elements in the Array created using Set are: [ 'GFG', 'JS' ]
The arr.forEach() method calls the provided function once for each element of the set where they are pushed to an array using the push() method.
Syntax:
 setName.forEach((ele)=>{
arrayName.push(ele);
});
Example: The below code example uses the forEach() method to convert a set into an array.
Javascript
let newSet = new Set();
let arr = [];
newSet.add( "Geeks" );
newSet.add( "for" );
newSet.add( "Geeks" );
console.log( "Set Elements: " , newSet);
let someFunction = function (val1) {
arr.push(val1);
};
newSet.forEach(someFunction);
console.log( "Array Elements: " + arr);
|
Output
Set Elements: Set(2) { 'Geeks', 'for' }
Array Elements: Geeks,for
Lodash is an JavaScript library that allows us to use the _.toArray() method which accepets a value and convert it to an array.
NOTE: To use this approach, you need to install the lodash library into your local system using the npm i lodash command.
Syntax:
const arrayName = _.toArray(setName);
Example: The below code implements the _.toArray() method of the lodash library to convert a set into an array.
Javascript
const _ = require( "lodash" );
let set = new Set([ 'welcome' , 'to' , 'GFG' ]);
console.log( 'Set Elements: ' , set);
console.log( 'Array Elements: ' , _.toArray(set));
|
Output:
Set Elements: Set(3) { 'welcome', 'to', 'GFG' }
Array Elements: [ 'welcome', 'to', 'GFG' ]
Last Updated :
18 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...