How to compare two JavaScript array objects using jQuery/JavaScript ? Last Updated : 13 Jul, 2023 Summarize Comments Improve Suggest changes Share 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 javascript-array javascript-object JavaScript-DSA JavaScript-Questions jQuery +2 More Similar Reads JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Introduction to Tree Data Structure Tree data structure is a hierarchical structure that is used to represent and organize data in the form of parent child relationship. The following are some real world situations which are naturally a tree.Folder structure in an operating system.Tag structure in an HTML (root tag the as html tag) or 15+ min read Introduction to JavaScript JavaScript is a versatile, dynamically typed programming language used for interactive web applications, supporting both client-side and server-side development, and integrating seamlessly with HTML, CSS, and a rich standard library.JavaScript is a single-threaded language that executes one task at 7 min read JSON Web Token (JWT) A JSON Web Token (JWT) is a standard used to securely transmit information between a client (like a frontend application) and a server (the backend). It is commonly used to verify users' identities, authenticate them, and ensure safe communication between the two. JWTs are mainly used in web apps an 7 min read Frontend Developer Interview Questions and Answers Frontend development is an important part of web applications, and it is used to build dynamic and user-friendly web applications with an interactive user interface (UI). Many companies are hiring skilled Frontend developers with expertise in HTML, CSS, JavaScript, and modern frameworks and librarie 15+ min read JavaScript Coding Questions and Answers JavaScript is the most commonly used interpreted, and scripted Programming language. It is used to make web pages, mobile applications, web servers, and other platforms. Developed in 1995 by Brendan Eich. Developers should have a solid command over this because many job roles need proficiency in Jav 15+ min read Top 95+ Javascript Projects For 2025 JavaScript is a lightweight, cross-platform programming language that powers dynamic and interactive web content. From real-time updates to interactive maps and animations, JavaScript brings web pages to life.Here, we provided 95+ JavaScript projects with source code and ideas to provide hands-on ex 4 min read Functions in JavaScript Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs.JavaScriptfunction sum(x, y) { return x + y; } console.log(sum(6, 9)); // output: 15Function Syntax 5 min read JavaScript Exercises, Practice Questions and Solutions JavaScript Exercise covers interactive quizzes, tracks progress, and enhances coding skills with our engaging portal. Ideal for beginners and experienced developers, Level up your JavaScript proficiency at your own pace. Start coding now! A step-by-step JavaScript practice guide for beginner to adva 3 min read Like