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.
<!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:
- 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.
<!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:
- 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:
<!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:
- Before Click on the Button:

- After Click on the Button:

Recommended Posts:
- Extract unique objects by attribute from array of objects.
- Compare two dates using JavaScript
- How to compare two arrays in JavaScript?
- How to check two objects have same data using JavaScript ?
- JavaScript Course | Objects in JavaScript
- Sort an array of objects using Boolean property in JavaScript
- How to remove duplicates from an array of objects using JavaScript ?
- How to remove object from array of objects using JavaScript ?
- How to convert JSON string to array of JSON objects using JavaScript ?
- How to merge properties of two JavaScript objects dynamically?
- Equality for two JavaScript objects
- Compare the Case Insensitive strings in JavaScript
- Optimum way to compare strings in JavaScript
- How to compare date part only without comparing time in JavaScript?
- Sort array of objects by string property value in JavaScript
- Max/Min value of an attribute in an array of objects in JavaScript
- How to remove Objects from Associative Array in JavaScript ?
- How to compare password and confirm password inputs using express-validator ?
- Creating objects in JavaScript (4 Different Ways)
- JavaScript | Objects
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.


