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

29 Jun, 2021
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 compare the equality of both array. 
html
<!DOCTYPE HTML> <html>  <head>     <title>         How to compare two JavaScript        array objects using jQuery ?    </title>         <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">    </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:  
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.  
html
<!DOCTYPE HTML> <html>  <head>     <title>         How to compare two JavaScript        array objects using jQuery ?    </title>         <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">    </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:  
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:  
html
<!DOCTYPE HTML> <html>  <head>     <title>         How to compare two JavaScript        array objects using jQuery ?    </title>         <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">    </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:  
Before Click on the Button:  
After Click on the Button:  
 
abhishek0719kadiyan
javascript-array
javascript-object
JavaScript
JQuery
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
JQuery | Set the value of an input text field
Form validation using jQuery
How to change selected value of a drop-down list using jQuery?
How to change the background color after clicking the button in JavaScript ?
How to fetch data from JSON file and display in HTML table using jQuery ?