By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
Responding to and Tracking Key Presses
How to Work With Responding to and Tracking Key Presses
JavaScript
Passing a JavaScript Value Between HTML Pages
Passing a JavaScript Value Between HTML Pages
JavaScript
Compare Objects in an Array
JavaScript Problem: Compare Objects in an Array
JavaScript
Switching Name Order Using Capturing Groups in Regular Expressions
Switching Name Order Using Capturing Groups in Regular Expressions
JavaScript
Shuffling an Array
JavaScript Problem: How to Perform Shuffling an Array
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > JavaScript Set intersection With Two Different Ways
JavaScript

JavaScript Set intersection With Two Different Ways

Admin
Last updated: 2022/10/12 at 7:46 AM
Admin
Share
2 Min Read
JavaScript Set Intersection

In This article, we will learn javascript set intersection with 2 different methods. 

Contents
what is an Intersection?Step to get the intersection of SetsMethod 1: JavaScript Set intersectionExplanation:Method 2: JavaScript Set intersectionCheck out our AmazingJavaScript Projects:

what is an Intersection?

In mathematical terms, this is expressed as an Intersection A ∩ B creates a set that contains those elements of a set A that is also in the set B. in simple words Intersection means elements common in both “A” and “B”.

Step to get the intersection of Sets

  1. Convert the first Set into an array.
  2. Use the filter() method to iterate over the array.
  3. Use the has() method to check if each value is contained in the second Set.
  4. Convert the array back to a Set.

Method 1: JavaScript Set intersection

let a = new Set([1,2,3]);
let b = new Set([4,3,2]);

let intersection = new Set(
    [...a].filter(x => b.has(x))
);

console.log(intersection); // Output: { 2, 3 }

Explanation:

Here We used the ES6 spread syntax (…) to convert the first Set to an array.

console.log([...a]); // [ 1, 2, 3 ]

After we use the filter() method to iterate over the array. On each iteration.

The filter() method creates a new array with satisfying the provided condition

we call the Set.has method to check if the value is contained in the second Set.

The has() method returns true if the value is found in the Set, and false otherwise.

In last we use Set() constructor to convert the array back to a Set.

Method 2: JavaScript Set intersection

Here you can also achieve the same thing using a loop over one of the Set objects, and check which elements are common between the two by using the has() method on the other Set. For example:

const setA = new Set([1, 2, 3]);
const setB = new Set([4, 3, 2, 6]);

const result = new Set();

for (const elem of setA) {
    if (setB.has(elem)) {
        result.add(elem);
    }
}

console.log(result); // Set(2) {2, 3}

Check out our AmazingJavaScript Projects:

  • How to Make Hangman Game in JavaScript
  • How to Make Password Generator In JavaScript
  • How to Make Dynamic Calendar in HTML & JavaScript

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Posted by Admin
Follow:
Rocoderes is a blog you can learn HTML, CSS, JavaScript, React Js and Python along with creative coding stuff and free source code files.
Previous Article Armstrong Number How to Find an Armstrong Number Using JavaScript
Next Article Palindrome Checker in HTML How to Make Palindrome Checker in HTML
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

Responding to and Tracking Key Presses

How to Work With Responding to and Tracking Key Presses

February 5, 2023
Passing a JavaScript Value Between HTML Pages

Passing a JavaScript Value Between HTML Pages

February 3, 2023
Compare Objects in an Array

JavaScript Problem: Compare Objects in an Array

January 30, 2023
Switching Name Order Using Capturing Groups in Regular Expressions

Switching Name Order Using Capturing Groups in Regular Expressions

January 29, 2023
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?