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

How to convert set to array in javascript

青灯夜游
Release: 2021-11-04 15:09:42
Original
13415 people have browsed it

Javascript method to convert set to array: 1. Use the spread operator "...", the syntax "array = [...setObject];"; 2. Use "Array.from()" Method, syntax "Array.from(setObject)".

How to convert set to array in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Javascript method to convert set (collection) into an array:

Method 1: Use the spread operator " ...

Syntax: var variablename = [...value];

Example:

const set =  new Set(['hello', 'world', '!']); 
console.log(set);
const array = [...set]; 
console.log(array);
Copy after login

How to convert set to array in javascript

Method 2: Use the Array.from() method

The Array.from() method retrieves data from an object or iterable object (such as Map, Set etc.) returns a new array.

Syntax: Array.from(arrayLike object);

Example:

const set =  new Set(['welcome',  'you','!']); 
console.log(set);
console.log(Array.from(set))
Copy after login

How to convert set to array in javascript

[Recommended learning: javascript advanced tutorial

The above is the detailed content of How to convert set to array in javascript. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template