Approach 1: Using jQuery not() method
- 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
<!DOCTYPE html> < html lang = "en" > < head > < script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js" > </ script > < title > How to compare two JavaScript array objects using jQuery/JavaScript ? </ title > </ head > < body style = "text-align:center;" > < h1 style = "color:green;" > w3wiki </ h1 > < h3 > Click on the button to check equality of arrays< br > Array 1 = [1, 3, 5, 8]< br > Array 2 = [1, 3, 5, 7] </ h3 > < button onclick = "GFG_Fun()" > Click Here </ button > < h3 id = "GFG" style = "color: green;" > </ h3 > < script > let elm = document.getElementById('GFG'); let arr1 = [1, 3, 5, 8]; let arr2 = [1, 3, 5, 7]; function GFG_Fun() { elm.innerHTML = $(arr1).not(arr2).length === 0 && $(arr2).not(arr1).length === 0; } </ script > </ body > </ html > |
Output:
How to compare two JavaScript array objects using jQuery/JavaScript ?
In this article, we are given two JavaScript array/array objects and the task is to compare the equality of both array objects.
These are the methods to compare two JavaScript array objects:
- Using jQuery not() method
- Use the sort() function
- Use JSON.stringify() function
- Using every() and indexOf()
- Using Lodash _.isEqual() Method