Home >Web Front-end >JS Tutorial >How to convert set to array in javascript
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)".
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);
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))
[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!