Created
April 30, 2020 01:45
-
-
Save mrm8488/a33c65cde26c3a3dd61a02d88483f1e6 to your computer and use it in GitHub Desktop.
Vanilla COMPARE JS OBJECTS
This file contains hidden or 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
const compareObjects = (a, b) => { | |
if (a === b) return true; | |
if (typeof a != 'object' || typeof b != 'object' || a == null || b == null) return false; | |
let keysA = Object.keys(a), keysB = Object.keys(b); | |
if (keysA.length != keysB.length) return false; | |
for (let key of keysA) { | |
if (!keysB.includes(key)) return false; | |
if (typeof a[key] === 'function' || typeof b[key] === 'function') { | |
if (a[key].toString() != b[key].toString()) return false; | |
} else { | |
if (!compareObjects(a[key], b[key])) return false; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment