Methods to convert set to array in es6: 1. Use the spread operator "...", the syntax "[...set object]"; 2. Use the Array.from() method, the syntax "Array.from(set object)".
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
In JavaScript, if you want to convert a Set (collection) into an Array array, you can do it in the following way.
Method 1: Use the spread operator (three-dot operator) " ...
"
Using the spread operator "..." can also help us convert a Set into an array.
Syntax:
var variablename = [...value];
Example:
<script> const set = new Set(['HELLO', 'JS']); console.log(set); const array = [...set]; console.log(array); </script>
Method 2: Use the Array.from() method.
The Array.from() method returns a new array from an object or iterable object (such as Map, Set, etc.).
Syntax:
Array.from(arrayLike object);
Example:
<script> const set = new Set(['welcome', 'you','!']); console.log(set); console.log(Array.from(set)) </script>