Created
July 9, 2021 16:14
-
-
Save RomanTurner/b29db1d17ef574ab8122be778ac4e462 to your computer and use it in GitHub Desktop.
Deep Equal comparison of two JavaScript 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 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