How to convert Set to Array in JavaScript?

A set can be converted to an array in JavaScript by the following way-

  • By using Array.from() method:
    This method returns a new Array from an array like object or iterable objects like Map, Set, etc.
    Syntax

    Array.from(arrayLike object);

    Example-1



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            Convert Set to Array
        </title>
    </head>
      
    <body>
        <center>
            <h1 style="color:green">
              GeeksforGeeks
          </h1>
            <script>
                const set = 
                   new Set(['welcome', 'to', 'GFG']);
                Array.from(set);
      
                document.write(Array.from(set));
            </script>
        </center>
    </body>
      
    </html>

    chevron_right

    
    

    Output

  • Using spread operator:
    Using of spread operator can also help us convert Set to array.
    Syntax

    var variablename = [...value]; 

    Example-2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            Convert Set to Array
        </title>
    </head>
      
    <body>
        <center>
            <h1 style="color:green">
              GeeksforGeeks
          </h1>
            <script>
                const set = 
                      new Set(['GFG', 'JS']);
                const array = [...set];
                document.write(array);
            </script>
        </center>
    </body>
      
    </html>

    chevron_right

    
    

    Output

  • Using forEach:
    Example-3:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
          Convert Set to Array
      </title>
    </head>
      
    <body>
        <center>
            <h1 style="color:green">
              GeeksforGeeks
          </h1>
      
            <script>
                var gfgSet = new Set();
                var gfgArray = [];
      
                gfgSet.add("Geeks");
                gfgSet.add("for");
                // duplicate item
                gfgSet.add("Geeks");
      
                var someFunction = function(
                val1, val2, setItself) {
                    gfgArray.push(val1);
                };
      
                gfgSet.forEach(someFunction);
      
                document.write("Array: " + gfgArray);
            </script>
        </center>
    </body>
      
    </html>

    chevron_right

    
    

    Output

Supported Browsers:

  • Google Chrome
  • Firefox
  • Edge
  • Opera
  • Apple Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.