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
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
includes() method takes the
value as an argument and returns true if a value is found in the array else it returns false.