Last active
September 5, 2021 17:32
-
-
Save grosscorporation/fbe7996cbd2cacf33b59499f20458a30 to your computer and use it in GitHub Desktop.
Compare two objects recursively, including DOM references and methods
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
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