Skip to content

Instantly share code, notes, and snippets.

@RomanTurner
Created July 9, 2021 16:14
Show Gist options
  • Save RomanTurner/b29db1d17ef574ab8122be778ac4e462 to your computer and use it in GitHub Desktop.
Save RomanTurner/b29db1d17ef574ab8122be778ac4e462 to your computer and use it in GitHub Desktop.
Deep Equal comparison of two JavaScript objects
const deepEqual = (object1, object2) =>{
const keys1 = Object.keys(object1);
const keys2 = Object.keys(object2);
if (keys1.length !== keys2.length) {
return false;
}
for (const key of keys1) {
const val1 = object1[key];
const val2 = object2[key];
const areObjects = isObject(val1) && isObject(val2);
if (
(areObjects && !deepEqual(val1, val2)) ||
(!areObjects && val1 !== val2)
) {
return false;
}
}
return true;
}
const isObject = (object) =>{
return object != null && typeof object === "object";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment