JavaScript Code


How to convert a Set into Array in JavaScript?


Convert a Set into Array in JavaScript

Given a set of values, write a JavaScript program to convert the set into an 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