How to Convert Set to Array in JavaScript
Overview
One of the most important components of JavaScript's ES6 release, sets offers the property of uniqueness by allowing for a maximum of one instance of each element. Despite how useful they are, you might still need to convert a set into an array in order to carry out specific operations that a set is unable to perform on a set. You might initially find this conversion to be difficult, but don't worry—there are other ways to transform a set into an array in Javascript.
Introduction
A structure called an array is used to represent a block of data (such as integers, objects, etc.) that has been allotted in sequential memory. A set is an abstract data type that can include different members without having to assign them in numerical order. Set items cannot be duplicated, although Array elements can.
We can transform a set into an array in javascript by using Array.From(), spread operator, and forEach() method. For these conversions, we will initialize a set that we will be converting into an array.
Learn more about JavaScript Array.
Learn more about Map and Set in 'JavaScript'.
Using Array.from() Method
Array.from() method returns a new array from an array like an object or an iterable object like a Map, Set, or another type of object. Iterable objects like Set can be converted into shallow-copied Array instances using the Array.from() method. With ES6, it was added to the Array object.
The syntax would seem as follows:
Syntax:
The example would seem as follows:
Example - 1: Creating a new instance of the array and converting the iterable into new array by using Array.from() method.
Output:
Explanation: We only gave an iterable object as a parameter to the Array.from() method. The method creates a new instance of the array and converts the iterable into an array.
Example - 2:
Output:
Using Spread Operator
The Spread operator can also be used to turn the set into an array. Where an array literal is expected, the spread syntax enables the expansion of the set. This was added to the JavaScript ES6 specification.
The syntax would seem as follows:
Syntax:
The example would seem as follows:
Example - 1: Converting a set into a new array by using spread operator.
Output:
Explanation:
All of the elements that the Set contains were included into a new array using the spread operator.
Example - 2:
Output:
Using forEach() Method
A different approach is to manually add each Set component to the array. The forEach() method makes this simple to implement, as demonstrated below:
The example would seem as follows:
Example - 1: Converting a set to array by using forEach() method.
Output:
Explanation: The function that we provided the Set. Each value in the Set triggers the forEach method to be called. We only push the value into the array after each iteration.
Example - 2:
Output:
Conclusion
- In this article, we have discussed various methods to convert of javascript set to an array.
- JavaScript offers several ways to convert a set to an array but forEach() method is more efficient because every element in the array is given a separate execution of the provided function by the forEach method.
- Set items cannot be duplicated, although Array elements can be duplicated.
- To achieve the best outcome, we can utilize either an Array or a Set. But occasionally you must convert Set to Array.