The Wayback Machine - http://web.archive.org/web/20220924193509/https://www.geeksforgeeks.org/how-to-compare-two-javascript-array-objects-using-jquery-javascript/amp/

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: 
 

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




<!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>

Output: 
 

Approach 2: 
 

Example:This example uses approach as discussed above. 
 




<!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>

Output: 
 

Approach 3: 
 

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




<!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>

Output: 
 

 




Article Tags :