Skip to content

Instantly share code, notes, and snippets.

@ToTheHit
Last active July 25, 2021 18:21
Show Gist options
  • Save ToTheHit/3d70977d36fc8b3f92139102714ecf9d to your computer and use it in GitHub Desktop.
Save ToTheHit/3d70977d36fc8b3f92139102714ecf9d to your computer and use it in GitHub Desktop.
const compareObjects = (obj1, obj2) => {
for (let p in obj1) {
if (obj1.hasOwnProperty(p) !== obj2.hasOwnProperty(p)) return false;
switch (typeof (obj1[p])) {
case 'object':
if (!compareObjects(obj1[p], obj2[p])) return false;
break;
case 'function':
if (typeof (obj2[p]) === 'undefined' || (p !== 'compare' && obj1[p].toString() !== obj2[p].toString())) return false;
break;
default:
if (obj1[p] !== obj2[p]) return false;
}
}
for (let p in obj2) {
if (typeof (obj1[p]) === 'undefined') return false;
}
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment