Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
Like Article
  • Last Updated : 19 Dec, 2022
Improve Article
Save Article
Like Article

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

Approach 1:  

  • Use the 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 returns false in both cases.
  • Also, check the length of both arrays.

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

html




<script src=
</script>
  
<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>


Output: 

 

Approach 2: 

  • First, use the 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 the Javascript`s sort() method to sort the array in ascending order then it checks for the same values.

html




<script src=
</script>
  
<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>


Output: 

 

Approach 3:  

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

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

Example: In this example, we will be using JSON.stringify() function to compare two array objects. 

html




<script src=
</script>
  
  
<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>


Output: 

 


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!