JavaScript Compare Two Arrays for Matches

JavaScript Compare Two Arrays for Matches

javascript compare two arrays for matches; In this post, you will learn how to compare two arrays in javascript returning matches in both arrays.

Work with javascript arrays and need to find matches values in both arrays, so this post is helpful for you because here you will learn How can find common elements or values in two arrays? 

JavaScript Compare Two Arrays for Matches

You have the following two arrays like:

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 you have elements in both the array that match each other or which elements are present in both the array in javascript. You want to get matches in both arrays.

Approach 1

See the following:

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().

Approach 2

See the following:

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().

Recommended JavaScript Posts:

If you have any queries or doubts on how to get match elements from two arrays in JavaScript, you can contact us using the comment box.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

One reply to JavaScript Compare Two Arrays for Matches

  1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *