JavaScript(JS) JS perform intersection between two arrays

www.igi‮tf‬idea.com

To perform an intersection between two arrays in JavaScript/JS, you can use the filter() method along with the includes() method.

Here is an example:

const array1 = [1, 2, 3, 4, 5];
const array2 = [3, 4, 5, 6, 7];

const intersection = array1.filter(value => array2.includes(value));

console.log(intersection); // Output: [3, 4, 5]

In the example above, we have two arrays: array1 and array2. To find their intersection, we use the filter() method on array1 and provide a callback function that checks whether each value in array1 is included in array2 using the includes() method. The resulting array will contain only the values that are present in both arrays.