JavaScript Code

How to convert a Set into Array in JavaScript?


Problem Statement

given a set, write a JavaScript program to convert a set into array

Solution

to convert given set of elements into an array in JavaScript, use the spread operator ... with the given set and enclose the expression in square brackets

[...mySet]

the above expression returns an array with the elements from the set

Program

1. convert the set vowelSet into an array vowelArray

const vowelSet = new Set(['a', 'e', 'i', 'o', 'u']);
const vowelArray = [...vowelSet];
console.log(vowelArray);


copyright @2022