GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — JavaScript - Sets
Selected Reading
JavaScript - SetsA JavaScript Set object is a collection of unique values. Each value can only occur once in a Set. A Set can hold any value of any data type. The sets are introduced to JavaScript in ECMAScript 6 (ES6). JavaScript Sets are similar to Arrays, but there are some key differences:
JavaScript Sets are often used to store unique values, such as the unique users who have visited a website. They can also be used to remove duplicate values from an Array. SyntaxUsers can follow the syntax below to define a set in JavaScript − let set1 = new Set([iterable]); In the above syntax, we used the Set() constructor function with a new keyword to define a set. Parameters
ExamplesExample (Access set elements)In the example below, we have passed the array containing the duplicate elements as an argument of the Set() constructor function. The num_set contains only unique values. We used the values() method to get the iterator of the set values. To traverse the iterator, we use the for...of loop. In the loop, we access the element and print it. You can observe that set contains unique values even if the input array contains duplicate elements. <html>
<body>
<p>The set elements are: </p>
<p id = "output"></p>
<script>
const num_set = new Set([10, 20, 20, 20]);
for (let value of num_set.values()) {
document.getElementById("output").innerHTML += value + "<br> ";
}
</script>
</body>
</html>
OutputThe set elements are: 10 20 Example (Insert elements into the sets)Users can use the add() method to insert the element into the set. Here, we have created the empty set. After that, we used the add() method 3 times to add the 60, 50, 50 elements into the set. We inserted the 50 for 2 times, but the set contains only once as it contains unique values. <html>
<body>
<p>Set elements after adding element/s to it: </p>
<div id = "output"> </div>
<script>
const num_set = new Set();
num_set.add(60);
num_set.add(50);
num_set.add(50);
for (let value of num_set.values()) {
document.getElementById("output").innerHTML += value + "<br> ";
}
</script>
</body>
</html>
OutputSet elements after adding element/s to it: 60 50 Example (Remove set elements)The delete() method of the set allows you to delete the element from the set. Here, we created the set containing the various numbers and used the delete() method to delete the 200 and 400 from the set. <html>
<body>
<p> After deleting elements from the set: </p>
<div id = "output"> </div>
<script>
let num_set = new Set([100, 200, 300, 400, 500]);
num_set.delete(200);
num_set.delete(400);
for (let value of num_set.values()) {
document.getElementById("output").innerHTML += value + "<br> ";
}
</script>
</body>
</html>
OutputThe set contains 100?: true Example (Check if the set contains a specific value)The example below demonstrates of using the has() method to check whether the set contains the specific value. Here, we check whether the set contains 100 and print the message in the output accordingly. <html>
<body>
<p id = "output">The set contains 100?: </p>
<script>
const num_set = new Set([100, 200, 300, 400, 500]);
document.getElementById("output").innerHTML += num_set.has(100);
</script>
</body>
</html>
OutputIt returns "true" because the element 100 is present in the set. The set contains 100?: true Mathematical set operationsThe Set class doesnt contain the built-in methods to perform the mathematical set operations like union, intersection, or set difference. So, you need to write the custom JavaScript code to perform the mathematical operations as shown below. Example (Union of two sets)The union of two sets contains all unique elements of set1 and set2. In the example below, set1 and set2 contain the car names. We have defined the union set and passed the array as an argument. We used the spread operator to create an array containing the elements of set1 and set2. <html>
<body>
<p>The Union of set1 and set2 is: </p>
<div id = "output"> </div>
<script>
const set1 = new Set(["BMW", "Audi", "TATA"]);
const set2 = new Set(["BMW", "TaTa", "Honda", "Suzuki"]);
const union = new Set([...set1, ...set2]); // Taking union
for (let value of union.values()) {
document.getElementById("output").innerHTML += value + "<br> ";
}
</script>
</body>
</html>
OutputIf we execute the program, it returns all unique elements of set1 and set2. The Union of set1 and set2 is: BMW Audi TATA TaTa Honda Suzuki Example (Intersection of two sets)The intersection of two sets contains the unique elements which exist in set1 and set2 both. Here, we have two sets containing the car names and defined the inter set to store the set elements which exist in both sets. We traverse through the elements of set1, and inside the loop we use the has() method to check whether the element of the set1 exists in the set2. If yes, we add an element into the inter set. <html>
<body>
<p> The intersection of set1 and set2 is: </p>
<p id = "output"> </p>
<script>
const set1 = new Set(["BMW", "Audi", "TATA"]);
const set2 = new Set(["BMW", "TATA", "Honda", "Suzuki"]);
const inter = new Set();
for (let car of set1) {
if (set2.has(car)) {
inter.add(car);
}
}
for (let value of inter.values()) {
document.getElementById("output").innerHTML += value + "<br> ";
}
</script>
</body>
</html>
OutputThe intersection of set1 and set2 is: BMW TATA Example (Difference of two sets)The difference between set1 and set2 contains all elements in the set1 but not in the set2. We have created the new set named diff and initialized it with the set1 elements. After that, we traverse the set2 elements. If any element of the set2 exists in the diff set, we delete it. <html>
<body>
<p>The difference of set1 and set2 is: </p>
<div id = "output"> </div>
<script>
const set1 = new Set(["BMW", "Audi", "TATA"]);
const set2 = new Set(["BMW", "TATA", "Honda", "Suzuki"]);
const diff = new Set(set1);
for (let car of set2) {
diff.delete(car);
}
for (let value of diff.values()) {
document.getElementById("output").innerHTML += value + "<br> ";
}
</script>
</body>
</html>
OutputThe difference of set1 and set2 is: Audi JavaScript Set ReferenceIn JavaScript, a Set is a collection of unique values. In other words, each value can occur only once within a set. It provides methods to add, remove, and check for the presence of elements in the set. Here, we have listed the properties and methods of the Set class. JavaScript Set Constructor()Following is the constructor of Set in JavaScript −
JavaScript Set PropertiesFollowing are the properties of Set class −
JavaScript Set MethodsIn the following table, we have listed all the properties of Set class −
Advertisements
| ||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |