Find the intersection of two arrays

Darren Jones
May 6, 2021

This function accepts two arrays as arguments and returns a new array of any elements that are in both arrays. This is the intersection of the arrays:


copy code
javascript
const intersection = (arrayA,arrayB) => arrayA.filter(value => arrayB.includes(value));

This works by using the filter method on the first array. This accepts a callback function that will only include any elements that return true for the condition provided. The condition is that the value must be in the other array. The includes method is used to check if this is the case.

Know a better answer? Join our our community and let us know.