Skip to content

Instantly share code, notes, and snippets.

@Shobhit1
Created September 12, 2016 16:15
Show Gist options
  • Save Shobhit1/142a6937526c06055b0b7d6cb6ab85cd to your computer and use it in GitHub Desktop.
Save Shobhit1/142a6937526c06055b0b7d6cb6ab85cd to your computer and use it in GitHub Desktop.
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