Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

How to Convert Array to Set in JavaScript?

  • Last Updated : 31 Jul, 2019

The task is to convert a JavaScript Array to a Set with the help of JavaScript. we’re going to discuss few techniques.

Approach:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • Take the JavaScript array into a variable.
  • Use the new keyword to create a new set and pass the JavaScript array as it first and only argument.
  • This will automatically create the set of the provided array.

Example 1: In this example, the array is converted into set using the same approach defined above.




<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        JavaScript 
      | Convert Array to Set.
    </title>
</head>
  
<body style="text-align:center;"
      id="body">
    <h1 style="color:green;" 
        id="h1">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP" 
       style="font-size: 15px; 
              font-weight: bold;">
    </p>
    <button onclick="GFG_Fun()">
        click here
    </button>
    <p id="GFG_DOWN" 
       style="color:green; 
              font-size: 20px;
              font-weight: bold;">
    </p>
    <script>
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
        var A = [1, 1, 2, 2, 2, 2, 5, 5];
        
        up.innerHTML = "Click on the button to convert"+
          " the array to set.<br>" + "Array - [" + A + "]";
  
        function GFG_Fun() {
            var set = new Set(A);
            down.innerHTML = JSON.stringify([...set]);
        }
    </script>
</body>
  
</html>

Output:

  • Before clicking on the button:
  • After clicking on the button:

Example 2: In this example, the array is converted into set using a bit approach than above.




<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        JavaScript 
      | Convert Array to Set.
    </title>
</head>
  
<body style="text-align:center;" id="body">
    <h1 style="color:green;" id="h1">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP" 
       style="font-size: 15px; 
              font-weight: bold;">
    </p>
    <button onclick="GFG_Fun()">
        click here
    </button>
    <p id="GFG_DOWN" 
       style="color:green; 
              font-size: 20px; 
              font-weight: bold;">
    </p>
    <script>
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
        
        var A = ["A", "A", "Computer Science", "portal", 
                 "for", "for", "Geeks", "Geeks"];
        
        up.innerHTML = "Click on the button to convert "+
          "the array to set.<br>" + "Array - [" + A + "]";
  
        function GFG_Fun() {
            var set = new Set(A);
            down.innerHTML = JSON.stringify([...set.keys()]);
        }
    </script>
</body>
  
</html>

Output:

  • Before clicking on the button:
  • After clicking on the button:



My Personal Notes arrow_drop_up
Recommended Articles
Page :