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

How to convert set to array in javascript

青灯夜游
青灯夜游Original
2021-11-04 15:09:4213430browse

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);

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

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!

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