JavaScript set to Array | Simple example code

Simplest way to convert set to Array is just spread the set inside [].

Other way convert set to Array in JavaScript:-

  • Array.from() method
  • Spread operator
  • forEach

Example of convert set to Array in JavaScript

Let’s see the one by one HTML example code for each method:-

1. Array.from() method

This method returns an Array object from any object with a length property or an iterable object.


JavaScript set to Array from method

2. Spread operator (…)

The spread operator is commonly used to make shallow copies of JS objects.

Output: Array(3) [ “A”, “B”, “C” ]

3. forEach method

The forEach() method calls a function once for each element in an array, in order.

Output: Array(3) [ 1, 2, 3 ]

Q: Is there a simple way to convert Set into Array?

Answer: Yes, Just use Array.from method or spread syntax.

Do comment if you have any doubts and suggestions on this topic.

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.