Created
November 14, 2021 06:09
-
-
Save airbr/dcc7f4756b8961822bedcfb167921d0e to your computer and use it in GitHub Desktop.
JavaScript deep object comparison
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
function deepEqual(a, b) { | |
if (a === b) return true; | |
if (a == null || typeof a != "object" || | |
b == null || typeof b != "object") 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) || !deepEqual(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