Convert set of string to a string array in JavaScript
This post will discuss how to convert a set of string to a string array in JavaScript.
To convert a set of strings to a string array in JavaScript, we need to understand the difference between a set and an array. A set is a collection of unique values, meaning that no value can be repeated in a set. An array is an ordered list of values, meaning that the values can be accessed by their index and can be repeated in an array.
1. Using spread operator
One way to convert a set of strings to a string array is to use the spread operator (…), which can spread the elements of an iterable object, such as a set, into an array literal. For instance:
|
1 2 3 4 |
let set = new Set(["apple", "banana", "cherry"]); let array = [...set]; console.log(array); // ["apple", "banana", "cherry"] |
Another way to convert a set of strings to a string array is to use the Array.from() function, which can create an array from any iterable object, such as a set. For instance:
|
1 2 3 4 |
let set = new Set(["apple", "banana", "cherry"]); let array = Array.from(set); console.log(array); // ["apple", "banana", "cherry"] |
Both of these functions will create a new array that contains the same values as the original set, but in the same order as they were inserted into the set. However, these functions will not work if the set contains nested objects or arrays, as they will not be copied by value, but by reference. In this case, modifying a nested element in one collection will also affect the other collection. For instance:
|
1 2 3 4 5 6 7 8 |
let set = new Set(["apple", ["banana", "cherry"], {name: "David"}]); // ["apple", ["banana", "cherry"], {name: "David"}] let array = [...set]; array[1][0] = "berry"; console.log(set.has(["berry", "cherry"])); // false console.log(set.has(["banana", "cherry"])); // true |
2. Using JSON functions
If we want to create a deep copy of a set of strings to a string array, we need to use a function that can recursively copy all the elements in the collection by value. One way to do this is to use the JSON.stringify() and JSON.parse() functions, which can convert an object to a JSON string and then parse it back to an object. For instance:
|
1 2 3 4 5 6 |
let set = new Set(["apple", ["banana", "cherry"], {name: "David"}]); let array = JSON.parse(JSON.stringify([...set])); // ["apple", ["banana", "cherry"], {name: "David"}] console.log(array); |
This function will create a new array that contains independent copies of all the elements in the original set. This means that if we modify an element in one collection, it will not affect the other collection. However, it has some limitations, such as not being able to copy functions, symbols, or circular references. Also, it can be slower than other functions.
That’s all about converting a set of string to a string array in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)