Horje
What is Set Intersection in JavaScript ?

In JavaScript, the concept of Set intersection refers to finding the common elements between two or more sets. The result of the intersection operation is a new Set containing only the elements that are common to all the input sets.

Example: Here, The spread operator ([...set1]) is used to convert the elements of set1 into an array. The filter() method is applied to the array, retaining only the elements that are present in set2 using the has() method.

Javascript

const set1 = new Set([1, 2, 3, 4]);
const set2 = new Set([3, 4, 5, 6]);
 
// Find the intersection of set1 and set2
const intersection = new Set([...set1]
    .filter(element => set2.has(element)));
 
console.log(intersection); // Output: Set { 3, 4 }

Output

Set(2) { 3, 4 }




Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to Create an Empty Set in JavaScript ? How to Create an Empty Set in JavaScript ?
What is Array.prototype.slice() method in JavaScript ? What is Array.prototype.slice() method in JavaScript ?
How to Append Header to a HTML Table in JavaScript ? How to Append Header to a HTML Table in JavaScript ?
Add Characters to a String in JavaScript Add Characters to a String in JavaScript
Split a String into an Array of Words in JavaScript Split a String into an Array of Words in JavaScript

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14