How to find the intersection of arrays in JavaScript
In this post we will give you information about How to find the intersection of arrays in JavaScript. Hear we will give you detail about How to find the intersection of arrays in JavaScriptAnd how to use it also give you demo for it if it is necessary.
we are going to learn about the intersection of two arrays in JavaScript with the help of examples.
Consider we have two arrays like this.
const arr1 = [1,2,3,4];const arr2 = [1,2,5,6];
The intersection of the above two arrays is [1,2].
Let’s solve this problem by using JavaScript filter() and includes() methods.
Example:
const arr1 = [1,2,3,4];const arr2 = [1,2,5,6];const intersection = arr1.filter(value=>arr2.includes(value));console.log(intersection); // [1,2]
In the above code, on each iteration, the filter method only kept the values that returns true.
The includes() method takes the value as an argument and returns true if a value is found in the array else it returns false.
Hope this code and post will helped you for implement How to find the intersection of arrays in JavaScript. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs