JavaScript offers a powerful data structure called Set, which allows for the storage of unique elements without duplication. However, there are situations where you may need to convert them to an Array to perform specific operations that cannot be done directly on a Set. In this article, we will learn how to change Set to Array in JavaScript.
What are Arrays in JavaScript?
Arrays are linear data structures that store homogenous data, i.e. same type of data. An array can contain all integers, all characters, or all strings as the elements. An array can not be a combination of integers and characters, or integers or strings. Let us look at how to declare and use arrays in Javascript:
//declaring an integers array
let marks = [98, 75, 62, 82, 97, 53];
//declaring a characters array
let grades=['A', 'B', 'A', '0'];
//declaring a strings array
let favtutors=["John", "Joshua", "Alex", "Kate"];
//declaring a boolean array
let present=[true, false, false, true];
In the code above, we have made integers array marks containing all integers, a characters array grade consisting of all elements as characters, a strings array ‘favtutors’ consisting of all strings, and a boolean present array consisting of all boolean values.
You can check out this article if you want to learn how to Convert a String to an Array in JavaScript.
What are Sets in JavaScript?
A set is a data type that stores unique values. No duplicate values are found in a set. It can hold either unique integers, unique characters, or unique strings. In a set, one cannot access any element with the help of an index. Here’s an example of how to create an empty set in JavaScript:
//creating an empty set in JavaScript let ids=new Set(); //printing the empty set console.log(ids);
Output:
Set(0) { }
Here 0 represents the number of values in the set. The { } represents the empty set.
You can also create a set with some unique values:
//creating a set with some values let ids=new Set([1,2,3]); //printing the set console.log(ids);
Output:
Set(3) { 1, 2, 3 }
Here 3 represents the number of elements in the set. We have obtained the desired result.
3 Methods to Convert Set to Array
In this section, we will discuss various techniques on how to convert a set to array in JavaScript:
1) Using the Array.from() Method
The Array.from() method is a built-in function in JavaScript that creates a new Array from an iterable or array-like object. It takes an iterable or array-like object as its parameter. It returns a new Array with the same elements as the input iterable object. This method supports the mapping function for element transformation and provides high clarity and readability.
To create a set from an array, simply make a set with desired elements and pass it as a parameter in Array.from() method. This will convert the set to an array. Store the array in a new variable. Let us see an example of the same:
//creating a set let ids=new Set([1,2,3,4,5]); //printing the set console.log(ids); //converting set to an array using Array.from() method let arrFromSet=Array.from(ids); //printing the array console.log(arrFromSet);
Output:
Set(5) { 1, 2, 3, 4, 5 }
[ 1, 2, 3, 4, 5 ]
In this example, we convert the set to an array using the Array.from() method. We have first printed the set and then the array which is formed from the set itself. We have obtained the desired output.
Although this method is simple, clear, readable, and efficient, it requires more code than the spread operator. Let us learn how to use the spread operator.
2) Using the Spread Operator
Another method of converting set to array in JavaScript is by using the spread operator (…). The spread operator allows us to expand elements from an iterable object, such as a Set, into a new Array. This method is concise and straightforward to implement.
To create a set from an array, simply make a set with desired elements. Use the spread operator (…) followed by the Set name inside square brackets to spread the elements into a new Array. Finally, store the array in a new variable. Let us see an example of the same:
//creating a set let ids=new Set([101, 102, 103]); //printing the set console.log(ids); //converting set to an array using the spread operator let arrFromSet=[...ids]; //printing the array console.log(arrFromSet);
Output:
Set(3) { 101, 102, 103 }
[ 101, 102, 103 ]
In this example, we convert the set to an array using the spread operator. We have first printed the set and then the array which is formed from the set itself. We have obtained the desired output.
Although this method is concise and easy to use, it does not support the mapping function during conversion.
3) Using the forEach() Method
Another method for converting a set to an array is the forEach() method. The forEach() is a built-in function in JavaScript that allows you to execute a provided function once for each element in an array or Set. We can iterate over the elements of a set using the forEach() method and push them into a new Array.
So, firstly create a Set with the desired elements. Use the forEach() method on the Set to iterate over it and provide a function that pushes each element into the Array. Store the array into a new variable. Let us see an example of the same.
//creating a set let ids=new Set([101, 102, 103]); //printing the set console.log(ids); //declaring an empty array let arrFromSet=[]; //using the forEach() method ids.forEach(element=>arrFromSet.push(element)); //printing the array console.log(arrFromSet);
Output:
Set(3) { 101, 102, 103 }
[ 101, 102, 103 ]
In this example, we convert the set to array using the forEach() method. We have first printed the set and then the array which is formed from the set itself. We have obtained the desired output.
This method allows greater control during conversion as we are able to perform additional operations on each element. But, Unlike previous methods, we have to perform the push operation in an array manually.
Comparison of the Three Methods
Now that we have gone through various methods of converting a set to an array in JavaScript, it’s time we compare them based on different factors.
Method | Complexity | Support for Mapping Function | Clarity and Readability |
Array.from() | O(n) | Yes | High |
Spread Operator | O(n) | No | High |
forEach() Method | O(n) | No | Medium |
The choice of method depends on your specific requirements and coding style.
- If one needs to apply a mapping function to each element during conversion, the Array.from() method is preferred.
- The spread operator is chosen when simplicity and conciseness are important.
- When one needs more control over each element to apply different operations, forEach() method is preferred.
Conclusion
In this article, we discussed the various techniques to convert a JavaScript Set to Array, using Array.from(), forEach() method, and the spread operator. We also provided a comparison of the methods along with their time complexities.