Option 1: Utilizing Array.from() methodology
arr = Array.from(aspect)
Element: An aspect may be a string, an object, or a set.
Arr: The variable we’ll use to maintain the results of our Array.from perform is known as arr.
Look on the following instance to perceive extra about this methodology:
<!DOCTYPE html> <html> <head> <title> Convert Set to Array </title> </head> <physique> <heart> <h1 fashion="color:green"> GeeksforGeeks </h1> <script> const set = new Set(['welcome', 'to', 'GFG']); Array.from(set); doc.write(Array.from(set)); </script> </heart> </physique> </html>
Option 2: Utilizing unfold operator
Utilizing unfold operator is also a nice resolution to convert set to array.
var variablename = [...value];
The unfold operator in the syntax above will goal all values in a particular variable. The unfold operator is useful once we need to copy, develop or mix math objects with different objects. Look on the instance under:
var s = new Set([2, 4, 6, 8]); let arr = [...s]; console.log(arr); /* Output: [ 2, 4, 6, 8 ] */
Option 3: Utilizing Set.prototype.forEach() perform
A distinct strategy is to add every Set element to the array individually.
obj.forEach((x) => //Statements for each aspect//);
In every iteration of the forEach() perform, x: is the worth assigned to every aspect.
obj: Object with objects being iterated; it may be an object, a string, a set, a map, or a perform.
The forEach() methodology makes this easy to accomplish, as proven under:
var s = new Set([2, 4, 6, 8]); let arr = ; s.forEach(x => arr.push(x)); console.log(arr); /* Output: [ 2, 4, 6, 8 ] */