JavaScript compare two arrays for matches; Through this tutorial, i am going to show you how to compare two arrays to find matches in JavaScript arrays.
Let, you have the following arrays in javaScript:
let arr1= [1,10,11,12,15,100,5,6,7,5];
let arr2= [1,10,11,12,15,100,50,60,70,50];
And want to find matching values in two arrays javascript; So, you can see the simple two ways to find matching values in two arrays javascript.
JavaScript Compare Two Arrays for Matches
Here, i will provide you two simple ways to find matching values in two arrays javascript; as shown below:
- Solution 1 – find matching values in two arrays javascript
- Solution 2 – find matching values in two arrays javascript
Solution 1 – find matching values in two arrays javascript
let arr1= [1,10,11,12,15,100,5,6,7,5]; let arr2= [1,10,11,12,15,100,50,60,70,50]; function arrayMatch(arr1, arr2) { var arr = []; arr1 = arr1.toString().split(',').map(Number); arr2 = arr2.toString().split(',').map(Number); console.log(arr1); // for array1 for (var i in arr1) { if(arr2.indexOf(arr1[i]) !== -1) arr.push(arr1[i]); } console.log(arr); return arr.sort((x,y) => x-y); } console.log(arrayMatch(arr1, arr2)); // [1, 10, 11, 12, 15, 100]
Here,
- First of declaring two arrays with elements.
- Declare a function, which is used to compare two arrays and find matches.
- first of all, Declare an empty array name arr.
- next split array by commas using split() method.
- iterate for-in loop over the array elements.
- Each iteration, matches first array elements to second array elements using indexOf() method, if they match then push elements into arr array.
- Sort arr array elements in ascending order and as well as return sorted array list.
- Call above defined function with arguments(arr1 and arr2).
- Print result using Console.log().
Solution 2 – find matching values in two arrays javascript
let arr1= [1,10,11,12,15,100,5,6,7,5]; let arr2= [1,10,11,12,15,100,50,60,70,50]; function arrayMatch(arr1, arr2) { var arr = []; // Array to contain match elements for(var i=0 ; i<arr1.length ; ++i) { for(var j=0 ; j<arr2.length ; ++j) { if(arr1[i] == arr2[j]) { // If element is in both the arrays arr.push(arr1[i]); // Push to arr array } } } return arr; // Return the arr elements } console.log(arrayMatch(arr1, arr2)); // [1, 10, 11, 12, 15, 100]
Here,
- First of declaring two arrays with elements.
- Declare a function, which is used to compare two arrays and find common elements.
- first of all, Declare an empty array name arr.
- iterate through all the elements in one of the arrays.
- Then, for each element in this array, iterate through all the elements of the other array.
- If the element is present in both array, then push this element to the arr array.
- Otherwise, continue with the next element.
- Call above defined function with arguments(arr1 and arr2).
- Print result using Console.log().
Thanks for the great and informative content. It contained very helpful information, which really made my work easier. Do keep posting more such amazing stuff.