Home  >  Article  >  Web Front-end  >  How to convert set to array in es6

How to convert set to array in es6

青灯夜游
青灯夜游Original
2022-03-07 17:41:303772browse

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)".

How to convert set to array in es6

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([&#39;HELLO&#39;, &#39;JS&#39;]); 
       console.log(set);
       const array = [...set]; 
       console.log(array); 
</script>

How to convert set to array in es6

##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([&#39;welcome&#39;,  &#39;you&#39;,&#39;!&#39;]); 
     console.log(set);
     console.log(Array.from(set))
</script>

How to convert set to array in es6

[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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn