Skip to content

Instantly share code, notes, and snippets.

@grosscorporation
Last active September 5, 2021 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grosscorporation/fbe7996cbd2cacf33b59499f20458a30 to your computer and use it in GitHub Desktop.
Save grosscorporation/fbe7996cbd2cacf33b59499f20458a30 to your computer and use it in GitHub Desktop.
Compare two objects recursively, including DOM references and methods
Object.compare = function (premise, supplement) {
for (let p in premise) {
if(premise.hasOwnProperty(p)){
if (premise.hasOwnProperty(p) !== supplement.hasOwnProperty(p)) return false;
switch (typeof (premise[p])) {
case 'object':
if (!Object.compare(premise[p], supplement[p])) return false;
break;
case 'function':
if (typeof (supplement[p]) == 'undefined' || (p !== 'compare' && premise[p].toString() !== supplement[p].toString())) return false;
break;
default:
if (premise[p] !== supplement[p]) return false;
}
}else{
return false
}
}
for (let p in supplement) {
if( supplement.hasOwnProperty(p) ){
if (typeof (premise[p]) == 'undefined') return false;
}else {
return false
}
}
return true;
}
const a = {
love: 34,
mirror: {love: 'Mayoral', secondary: 2}
}
const b = {
love: 34,
mirror: {love: 'Mayoral', secondary: 2}
}
console.log(Object.compare( a, b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment