How to compare two JavaScript array objects using jQuery/JavaScript ?

Given two JavaScript array/array objects and the task is to compare the equality of both array objects.

Approach 1:

  • Use jQuery not() method to check for each element of array1, if it is not present in array2 or for each element of array2, if this is not present in array1, then it return false in both cases.
  • Also check the length of both arrays.

Example: This example uses jQuery not() method to comapre the equality of both array.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>  
<html>  
  
<head>  
    <title>  
        How to compare two JavaScript
        array objects using jQuery ?
    </title>
      
    <script src=
    </script>
</head>  
  
<body style = "text-align:center;">  
      
    <h1 style = "color:green;" >  
        GeeksForGeeks  
    </h1
      
    <p id = "GFG_UP" style
        "font-size: 15px; font-weight: bold;"
    </p>
      
    <button onclick = "GFG_Fun()">
        click here
    </button>
      
    <p id = "GFG_DOWN" style
        "font-size: 24px; font-weight: bold; color: green;"
    </p>
      
    <script>  
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
        var arr1 = [1, 3, 5, 8];
        var arr2 = [1, 3, 5, 7];
      
        up.innerHTML = "Click on the button to check "
                + "equality of arrays.<br>Array1 - " 
                + arr1 + "<br>Array2 - " + arr2; 
      
        function GFG_Fun() {
            down.innerHTML = 
                    $(arr1).not(arr2).length === 0 &&
                    $(arr2).not(arr1).length === 0;
        
    </script>  
</body>  
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Approach 2:

  • First, use sort() function to sort both arrays in ascending order.
  • Then start matching the element one by one and if there is any mismatch found then it returns false otherwise it returns true.

Example:This example uses approach as discussed above.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>  
<html>  
  
<head>  
    <title>  
        How to compare two JavaScript
        array objects using jQuery ?
    </title>
      
    <script src=
    </script>
</head>  
  
<body style = "text-align:center;">  
      
    <h1 style = "color:green;" >  
        GeeksForGeeks  
    </h1
      
    <p id = "GFG_UP" style
        "font-size: 15px; font-weight: bold;"
    </p>
      
    <button onclick = "GFG_Fun()">
        click here
    </button>
      
    <p id = "GFG_DOWN" style
        "font-size: 24px; font-weight: bold; color: green;"
    </p>
      
    <script>  
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
        var arr1 = [1, 3, 7, 5];
        var arr2 = [1, 3, 5, 7];
          
        up.innerHTML = "Click on the button to check "
                + "equality of arrays.<br>Array1 - " 
                + arr1 + "<br>Array2 - " + arr2;
          
        function compare(ar1, ar2) {
            ar1.sort();
            ar2.sort();
              
            if(ar1.length != ar2.length)
                return false;
              
            for(var i = 0; i < ar1.length; i++) {
                if (ar1[i] != ar2[i])
                    return false;
            }
            return true;
        }
        function GFG_Fun() {
            down.innerHTML = compare(arr1, arr2);
        
    </script>  
</body>  
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Approach 3:

  • Create two array objects and store it into arr1 and arr2 variables.
  • Use JSON.stringify() function to convert object into JSON string.
  • Now compare both JSON string using comparison operator (==) to check both array objects are equal or not.

Note: This method works only when both array objects sorted into the same fashion.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>  
<html>  
  
<head>  
    <title>  
        How to compare two JavaScript
        array objects using jQuery ?
    </title>
      
    <script src=
    </script>
</head>  
  
<body style = "text-align:center;">  
      
    <h1 style = "color:green;" >  
        GeeksForGeeks  
    </h1
      
    <h2>
        Click on the button to check
        equality of arrays
    </h2>
      
    <p id = "GFG_UP" style
        "font-size: 15px; font-weight: bold;"
    </p>
      
    <button onclick = "GFG_Fun()">
        click here
    </button>
      
    <p id = "GFG_DOWN" style
        "font-size: 24px; font-weight: bold; color: green;"
    </p>
      
    <script>  
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
          
        var arr1 = [
            {id: "1", name: "GeeksforGeeks"}, 
            {id: "2", name: "GFG"}
        ]; 
          
        var arr2 = [
            {id: "1", name: "GeeksforGeeks"},
            {id: "2", name: "GFG"}
        ]; 
          
        up.innerHTML = "<h3>Array1:</h3> " 
                    + JSON.stringify(arr1)
                    + "<h3>Array2:</h3> " 
                    + JSON.stringify(arr2);
          
        function compare(ar1, ar2) {
            if(JSON.stringify(arr1) == JSON.stringify(arr2)){
                return true;
            }
            else
                return false;
        }
        function GFG_Fun() {
            down.innerHTML = compare(arr1, arr2);
        
    </script>  
</body>  
  
</html>

chevron_right


Output:

  • Before Click on the Button:
  • After Click on the Button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.