How to compare two JavaScript array objects using jQuery/JavaScript ? Last Updated : 13 Jul, 2023 Comments Improve Suggest changes Like Article Like Report 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() methodUse the sort() functionUse JSON.stringify() functionUsing every() and indexOf()Using Lodash _.isEqual() MethodApproach 1: Using jQuery not() methodUse 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;"> GeeksforGeeks </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: Approach 2: Use the sort() functionFirst, 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 JavaScript`s sort() method to sort the array in ascending order then it checks for the same values. JavaScript let arr1 = [1, 3, 7, 5]; let arr2 = [1, 3, 5, 7]; function compare(ar1, ar2) { ar1.sort(); ar2.sort(); if (ar1.length != ar2.length) return false; for (let i = 0; i < ar1.length; i++) { if (ar1[i] != ar2[i]) return false; } return true; } console.log(compare(arr1, arr2)); OutputtrueApproach 3: JSON.stringify() functionCreate 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. JavaScript let arr1 = [ { id: "1", name: "GeeksforGeeks" }, { id: "2", name: "GFG" } ]; let arr2 = [ { id: "1", name: "GeeksforGeeks" }, { id: "2", name: "GFG" } ]; function compare(ar1, ar2) { if (JSON.stringify(ar1) == JSON.stringify(ar2)) { return true; } else return false; } console.log(compare(arr1, arr2)); OutputtrueApproach 4: Using every() and indexOf()We can use every() to iterate through every array element. The JavaScript Array indexOf() Method is used to find the index of the first occurrence of the search element provided as the argument to the method. Example: JavaScript let array1 = [1, 2, 3]; let array2 = [1, 2, 3]; let isEqual = array1.length === array2.length && array1.every(function (element, index) { return element === array2[index]; }); console.log(isEqual); OutputtrueApproach 5: Using Lodash _.isEqual() MethodThe Lodash _.isEqual() Method performs a deep comparison between two values to determine if they are equivalent. Example: JavaScript // Defining Lodash variable const _ = require('lodash'); let val1 = { "a": "gfg" }; let val2 = { "a": "gfg" }; // Checking for Equal Value console.log("The Values are Equal : " +_.isEqual(val1,val2)); Output: The Values are Equal : true Comment More infoAdvertise with us Next Article How to compare two JavaScript array objects using jQuery/JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JQuery javascript-array javascript-object JavaScript-DSA JavaScript-Questions jQuery +4 More Similar Reads How to compare Arrays of Objects in JavaScript? In JavaScript, comparing arrays of objects can be more complex than comparing primitive data types. We will discuss different ways to compare arrays of objects effectively, with detailed code examples and explanations.Syntax: Before going to detail the comparison techniques, let's first understand h 5 min read How to check two elements are same using jQuery/JavaScript ? Given an HTML document containing two elements and the task is to check whether both elements are same or not with the help of JavaScript. Approach 1: Use is() method to check both selected elements are same or not. It takes an element as argument and check if it is equal to the other element. Examp 2 min read How to Compare Objects in JavaScript? Comparing objects is not as simple as comparing numbers or strings. Objects are compared based on their memory references, so even if two objects have the same properties and values, they are considered distinct if they are stored in different memory locations. Below are the various approaches to co 3 min read How to convert form data to JavaScript object with jQuery ? JavaScript objects are complex data types that can store multiple values and properties. Unlike primitive data types that only hold a single value, objects can hold various data types, including other objects, arrays, functions, and more. The inputs given by a user can be converted into objects wher 3 min read How to check two objects have same data using JavaScript ? In JavaScript, we cannot directly compare two objects by equality operators (double equals == or triple equals ===) to see whether they are equal or not. Comparing two objects like this results in false even if they have the same data. It is because those are two different object instances, they are 2 min read How to Swap Two Array of Objects Values using JavaScript ? Swapping values between two arrays of objects is a common operation in JavaScript, there are multiple methods to swap values between arrays of objects efficiently. Swapping values between arrays can be useful in scenarios like reordering data, shuffling items, or performing certain algorithms. These 6 min read How to Compare Two Objects using Lodash? To compare two objects using lodash, we employ Lodash functions such as _.isEqual(), _.isMatch(), and _.isEqualWith(). These methods enable us to do a comparison between two objects and determine if they are equivalent or not.Below are the approaches to do a comparison between two objects using Loda 4 min read How to Sort an Array of Objects Based on a Key in JavaScript ? In JavaScript, sorting an array of objects based on the key consists of iterating over the array, applying the sort() method or other approaches, and arranging the array in the desired sorting order. Table of Content Using sort() methodUsing Custom Sorting FunctionUsing Lodash _.orderBy() MethodUsin 3 min read How to Merge two Different Arrays of Objects with Unique Values in JavaScript? Merging two arrays of objects with unique values is a common task in JavaScript. This operation involves combining the elements of two arrays while ensuring that each resulting object has a unique identifier. We will explore various approaches to merging two arrays of objects with unique values. The 4 min read JavaScript - Sort JS Arrays of Objects by Date Key The following approaches can be used to sort the array of objects by Date as KeyUsing Array.sort() with Comparison FunctionUse the comparison function to sort the array of objects (complex array).JavaScriptconst arr = [ { name: 'Geeks', date: '2022-03-15' }, { name: 'Abc', date: '2022-03-12' }, { na 3 min read Like