HomeTutorsContact

How to convert Set to Array in JavaScript?

By Gulshan Saini
Published in JavaScript
October 28, 2020
1 min read

Let’s say you have following Set of numbers, which you want to convert to array.

let set = new Set([1, 1, 3, 4, 3, 5, 6, 7, 8, 9, 0, 11, 12, 3])

One way of converting a Set to Array is using Array.from() method

The Array.from() static method creates a new, shallow-copied Array instance from an array-like or iterable object.

let array = Array.from(set)

Another, way of converting a Set to array is using spread operator.

To convert set variable we will wrap it inside square brackets and spread the variable as follows

let array = [...set]

Both the approach work fine and the results are same.

Output

console.log(array) // (11) [1, 3, 4, 5, 6, 7, 8, 9, 0, 11, 12]

Tags

#javascript
Previous Article
How to get all unique values in a JavaScript array?

Related Posts

JavaScript
How to check if a string contains a substring in JavaScript?
January 05, 2021
1 min
Gulshan Saini

Gulshan Saini

Fullstack Developer

Topics

Flutter
JavaScript
NextJS
ReactJS
Typescript

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.
© 2021, All Rights Reserved.

Quick Links

Contact UsBrowserCSSPythonPuppeteer

Social Media