To convert a set to an array in JavaScript, you can use any of these methods –
- Use the Array.from() method
- Use the spread operator (…)
- Use the Array.forEach() method
Let’s look at each of these methods in detail below.
Convert A Set To An Array In JavaScript
![convert a set to an array in javascript](https://typedarray.org/wp-content/uploads/2022/07/convert-set-to-array-js-683x1024.jpg)
1. Use The Array.from() Method
The Array.from() method creates a new array from an iterable object.
Syntax
Array.from(iterableObj, mapFn, thisArg)
Parameters
iterableObj: It is an iterable object whose elements are to be mapped.
mapFn: It is an optional parameter. It is a map function which is invoked on every element of the iterableObj. The result of this map function is stored in an output array.
thisArg: It is an optional parameter. Value to use as this when executing mapFn.
Return value
A new array instance.
Example
In the following example, we have a Set object containing some fruits name.
We use the Array.from() method to convert this set into an array.
var fruits = new Set(["Apple", "Banana", "Mango"]);
var arr = Array.from(fruits);
console.log(arr);
Output
[“Apple”, “Banana”, “Mango”]
In the above code, we have created a Set object fruits. This set object contains three string elements – Apple, Banana, and Mango.
Then we use the Array.from() method to convert this set into an array. The result is logged onto the console which displays the converted array – [‘Apple’, ‘Banana’, ‘Mango’].
2. Use The Spread Operator (…)
The spread operator can be used to convert a set into an array.
Syntax
…spreadSet
Parameters
spreadSet: It is a set object whose elements are to be mapped.
Return value
A new array instance.
Example 1
In the following example, we have a Set object containing some fruits name. We use the spread operator (…) to convert this set into an array.
var fruits = new Set(["Apple", "Banana", "Mango"]);
var arr = [...fruits];
console.log(arr);
Output
[“Apple”, “Banana”, “Mango”]
Example 2
In the following example, we have a Set object containing some numbers. We use the spread operator (…) to convert this set into an array.
Then we use the sort() method to sort this array in ascending order.
var num = new Set([5, 10, 15, 20, 25]);
var arr = [];
arr = [...num];
console.log(arr);
Output
[5, 10, 15, 20, 25]
3. Use The Array.forEach() Method
The forEach() method is used to execute a given function for each element in an array.
Syntax
arr.forEach(callback(currentValue, index, arr), thisArg)
Parameters
callback: It is required and it is a function which is invoke for each element.
currentValue: It is required and it is the current element being processed in the array.
index: It is optional and it is the index of the current element being processed in the array.
arr: It is optional and it is the array for which forEach() method was called upon.
thisArg: It is optional and it is the value to use as this when executing callback.
Return value
undefined.
Example 1
In the following example, we have a Set object containing some fruits name. We convert this set into an array using the Array.forEach() method and log it onto the console.
var fruits = new Set(["Apple", "Banana", "Mango"]);
var arr = [];
fruits.forEach((value) => {
arr.push(value);
});
console.log(arr);
Output
[“Apple”, “Banana”, “Mango”]
Example 2
In the following example, we have a Set object containing some numbers. We convert this set into an array using the Array.forEach() method and sort it in descending order.
var num = new Set([5, 10, 15, 20, 25]);
var arr = [];
num.forEach((value) => {
arr.push(value);
});
console.log(arr.sort((a,b)=> b-a));
Output
[25, 20, 15, 10, 5]
Conclusion
In this article, we have learned how to convert a set to an array in JavaScript. We have also learned about the different methods which can be used to achieve this conversion.
I hope you found this article helpful and if you have any questions or comments, please feel free to post them below. Thanks!