How to check two objects have same data using JavaScript ? - GeeksforGeeks

01 Jul, 2020
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 referring to two different objects.
There is no direct method in javascript to check whether two objects have the same data or not.
Below is the pseudo-code of the function, followed by the code snippet:
Pseudo Code:
function hasSameData
    if both objects have the same number of keys (properties)
        if every key of obj1 matches with the corresponding key 
        of obj2 and values of the same keys of both objects match.
            return true
    return false

Approach:
We will find keys of both objects by Object.keys(), which returns an array of keys of that object.
For checking that every key in obj1 is also present in obj2, and if values of those key matches, we will use every() method. The every() method accepts a callback and returns “true” or “false” according to the callback condition.
Example:
javascript
<script>    const obj1 = {        name: 'Ram',        age: 21    };      const obj2 = {        name: 'Ram',        age: 21    };      const haveSameData = function (obj1, obj2) {        const obj1Length = Object.keys(obj1).length;        const obj2Length = Object.keys(obj2).length;          if (obj1Length === obj2Length) {            return Object.keys(obj1).every(                key => obj2.hasOwnProperty(key)                    && obj2[key] === obj1[key]);        }        return false;    }    document.write(haveSameData(obj1, obj2));</script>
Output:
true

Note: The above approach does not work for nested objects (Objects and arrays inside anobject). In such cases, it needs function according to the nested object.
Applying the above function would fail in case of nested objects as shown below:  
javascript
<script>    const obj1 = {        name: 'Ram',        age: 21,        hobbies: ['Cricket', 'Swimming']    };      const obj2 = {        name: 'Ram',        age: 21,        hobbies: ['Cricket', 'Swimming']    };    const haveSameData = function(obj1, obj2) {        const obj1Length = Object.keys(obj1).length;        const obj2Length = Object.keys(obj2).length;          if(obj1Length === obj2Length) {            return Object.keys(obj1).every(                key => obj2.hasOwnProperty(key)                   && obj2[key] === obj1[key]);        }        return false;    }    document.write(haveSameData(obj1, obj2));</script>
Output:
false

prerak_jain
JavaScript-Misc
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
How to get character array from string in JavaScript?
How to detect browser or tab closing in JavaScript ?
How to get selected value in dropdown list using JavaScript ?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?