Created
September 12, 2016 16:15
-
-
Save Shobhit1/142a6937526c06055b0b7d6cb6ab85cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const deepCompareObjects = (obj1, obj2) => { | |
// Create arrays of property names | |
const obj1Props = Object.getOwnPropertyNames(obj1) | |
const obj2Props = Object.getOwnPropertyNames(obj2) | |
// If number of properties is different, objects are not equivalent | |
if (obj1Props.length !== obj2Props.length) { | |
return false | |
} | |
for (let i = 0; i < obj1Props.length; i++) { | |
const propName = obj1Props[i] | |
// If values of same property are not equal, objects are not equivalent | |
if (obj1[propName] !== obj2[propName]) { | |
return false | |
} | |
} | |
// If we made it this far, objects are considered equivalent | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment