Skip to content

Instantly share code, notes, and snippets.

@mrm8488
Created April 30, 2020 01:45
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 mrm8488/a33c65cde26c3a3dd61a02d88483f1e6 to your computer and use it in GitHub Desktop.
Save mrm8488/a33c65cde26c3a3dd61a02d88483f1e6 to your computer and use it in GitHub Desktop.
Vanilla COMPARE JS OBJECTS
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