Last active
February 21, 2022 22:52
-
-
Save blarfoon/4ea7c0f4382c005a3a15850bf96f9e45 to your computer and use it in GitHub Desktop.
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
function areEqualDeep(a, b) { | |
if (typeof a == "object" && a != null && typeof b == "object" && b != null) { | |
const count = [0, 0]; | |
for (const key in a) count[0]++; | |
for (const key in b) count[1]++; | |
if (count[0] - count[1] != 0) { | |
return false; | |
} | |
for (const key in a) { | |
if (!(key in b) || !areEqualDeep(a[key], b[key])) { | |
return false; | |
} | |
} | |
for (const key in b) { | |
if (!(key in a) || !areEqualDeep(b[key], a[key])) { | |
return false; | |
} | |
} | |
return true; | |
} else { | |
return a === b; | |
} | |
} | |
const myFirstObject = {}; | |
const mySecondObject = {}; | |
console.log(areEqualDeep(myFirstObject, mySecondObject)); // > true | |
myFirstObject.hi = "mom"; | |
mySecondObject.hi = "mom"; | |
console.log(areEqualDeep(myFirstObject, mySecondObject)); // > true | |
myFirstObject.nested = { | |
hi: "medium", | |
}; | |
mySecondObject.nested = { | |
hi: "medium", | |
}; | |
console.log(areEqualDeep(myFirstObject, mySecondObject)); // > true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment