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

How to convert set to array in es6

青灯夜游
Release: 2022-03-07 17:43:37
Original
3859 people have browsed it

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];
Copy after login

Example:

<script> 
       const set =  new Set([&#39;HELLO&#39;, &#39;JS&#39;]); 
       console.log(set);
       const array = [...set]; 
       console.log(array); 
</script>
Copy after login

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);
Copy after login

Example:

<script> 
     const set =  new Set([&#39;welcome&#39;,  &#39;you&#39;,&#39;!&#39;]); 
     console.log(set);
     console.log(Array.from(set))
</script>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template