Skip to content

Instantly share code, notes, and snippets.

@blarfoon
Last active February 21, 2022 22:52
Show Gist options
  • Save blarfoon/856e1e425d42d0e182b49eedac3fc22c to your computer and use it in GitHub Desktop.
Save blarfoon/856e1e425d42d0e182b49eedac3fc22c to your computer and use it in GitHub Desktop.
function areEqualShallow(a, b) {
for (const key in a) {
if (!(key in b) || a[key] !== b[key]) {
return false;
}
}
for (const key in b) {
if (!(key in a) || a[key] !== b[key]) {
return false;
}
}
return true;
}
const myFirstObject = {};
const mySecondObject = {};
console.log(areEqualShallow(myFirstObject, mySecondObject)); // > true
myFirstObject.hi = "mom";
mySecondObject.hi = "mom";
console.log(areEqualShallow(myFirstObject, mySecondObject)); // > true
myFirstObject.nested = {
hi: "medium",
};
mySecondObject.nested = {
hi: "medium",
};
console.log(areEqualShallow(myFirstObject, mySecondObject)); // > false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment