function areEqualShallow(a, b) {
for(var key in a) {
if(!(key in b) || a[key] !== b[key]) {
return false;
}
}
for(var key in b) {
if(!(key in a) || a[key] !== b[key]) {
return false;
}
}
return true;
}
Notes:
Since this is shallow,
areEqualShallow({a:{}}, {a:{}})
is false.areEqualShallow({a:undefined}, {})
is false.This includes any properties from the prototype.
This uses
===
comparison. I assume that is what you want.NaN === NaN
is one case that may yield unexpected results. If===
is not what you want, substitute with the comparison you want.
EDIT: If the same keys are in each object, then
function areEqualShallow(a, b) {
for(var key in a) {
if(a[key] !== b[key]) {
return false;
}
}
return true;
}