How to Find Matching Values in Two Arrays Using JavaScript?

Estimated read time 1 min read

To find matching values in two arrays, you can use the filter method and compare each element in one array to elements in the other array. The matching values can then be added to a new array.

Here’s an example:

let array1 = [1, 2, 3, 4, 5];
let array2 = [2, 4, 6, 8, 10];

let matches = array1.filter(value => array2.includes(value));

console.log(matches);

In this example, two arrays array1 and array2 are defined. The code then uses the filter method to create a new array matches that contains only the values from array1 that are also included in array2. The resulting array of matching values is logged using console.log(matches).

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply