Assuming the objects in the example above are not equal and that the definition of "equal" above is incomplete (and assuming we want to handle objects only) the following should work:
function equals(o1, o2) {
if (typeof o1 !== 'object' || typeof o2 !== 'object') {
return false; // we compare objects only!
}
// when one object has more attributes than the other - they can't be eq
if (Object.keys(o1).length !== Object.keys(o2).length) {
return false;
}
for (let k of Object.keys(o1)) {
if (o1[k] !== o2[k]) {
return false;
}
}
return true;
}
To clarify, according to the definition in the question the following objects are equal:
o1 = { a: 1 };
o2 = { a: 1, b: 2 };
but I find it hard to believe that this was the intention of the OP!