Navigate back to the homepage

How to find common elements in two arrays javascript?

Infinitbility
JavaScript
May 22nd, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

Today, I’m going to show you how do I find common elements in two arrays of javascript, here I will use javascript filter() to iterate the array and return a new array based on condition, and includes() to check string is available in an array or not.

  • The filter() method creates a new array with all elements that pass the test implemented by the provided function.

  • The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

Well, let’s start today’s topic How to find common elements in two arrays javascript?

Okay, let’s plan what we are going to do in code.

  1. Create sample array variables.
  2. To get common elements in new array use the filter() method
  3. Use the includes() method to check string is available in arrays
1// Create sample arrays variables.
2const domainsArr1 = ["infinitbility", "aGuideHub", "SortoutCode"];
3const domainsArr2 = ["notebility", "aGuideHub", "repairbility"];
4
5// To get common elements in new array use `filter()` method
6// Use `includes()` method to check string is available in array
7const filteredArray = domainsArr1.filter(value => domainsArr2.includes(value));
8
9console.log("filteredArray", filteredArray)
10// Expected 'filteredArray' [ 'aGuideHub' ]

In the above example I used filter() and includes() both methods to create a new array of matched elements.

Here, as per expectaion it should show 'filteredArray' [ 'aGuideHub' ].

let’s check the output.

javascript, find common elements in two arrays example
javascript, find common elements in two arrays example

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail infinitbility[email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to find consecutive repeated characters in a string in javascript?

Example of find consecutive repeated characters in a string in javascript with step by step guide.

May 22nd, 2022 · 1 min read

How to find and remove element from array in javascript?

Example of find and remove element from array in javascript with step by step guide.

May 21st, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]