Home >Web Front-end >JS Tutorial >How to convert set to array in es6
Method to convert set into array in es6: 1. Use the spread operator "...", syntax "[...set object]"; 2. Use Array.from() method, 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 expansion operator (three-dot operator) "...
"
Use the expansion operator "... ” can also help us convert Set to 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.). Grammar:Array.from(arrayLike object);Example:
<script> const set = new Set(['welcome', 'you','!']); console.log(set); console.log(Array.from(set)) </script>[Related recommendations:
javascript video tutorial, web front end】
The above is the detailed content of How to convert set to array in es6. For more information, please follow other related articles on the PHP Chinese website!