Compare 2 objects and return true only if two values are the same. If they are more than two same values or all or none return false. Example:
A = {a: 1, b: 2, c: 3};
B = {a: 1, b: 5, c: 7};
C = {a: 1, b: 2, c: 7};
A and B should return true because A.a and B.a are the same. A and C should return false because A.a and C.a and A.b and C.b are the same.
So far I have this function:
But in the case, const ObB7
returns true and should be false.
And can this function be simplified?
function compareTwoObjects(ObA, ObB) {
const { a, b, c } = ObA;
const { a:d, b:e, c:f } = ObB;
if (
((a === d && (a !== e && a !== f))
|| (a === e && (a !== d && a !== f))
|| (a === f && (a !== e && a !== d)))
|| ((b === d && (b !== e && b !== f))
|| (b === e && (b !== d && b !== f))
|| (b === f && (b !== e && b !== d)))
|| ((c === d && (c !== e && c !== f))
|| (c === e && (c !== d && c !== f))
|| (c === f && (c !== e && c !== d)))
) {
return true;
}
return false;
}
const ObA = {a: 1, b: 2, c: 3};
const ObB0 = {a: 4, b: 5, c: 6}; // false
const ObB1 = {a: 4, b: 4, c: 4}; // false
const ObB2 = {a: 1, b: 1, c: 1}; // false
const ObB3 = {a: 2, b: 2, c: 2}; // false
const ObB4 = {a: 3, b: 3, c: 3}; // false
const ObB5 = {a: 1, b: 1, c: 7}; // false
const ObB6 = {a: 7, b: 2, c: 2}; // false
const ObB7 = {a: 7, b: 3, c: 3}; // false
const ObB8 = {a: 2, b: 3, c: 4}; // Should be false
const ObB9 = {a: 3, b: 7, c: 3}; // false
const ObB10 = {a: 5, b: 2, c: 3}; // true
const ObB11 = {a: 1, b: 5, c: 6}; // true
const ObB12 = {a: 0, b: 5, c: 3}; // true
console.log(compareTwoObjects(ObA, ObB0));
console.log(compareTwoObjects(ObA, ObB1));
console.log(compareTwoObjects(ObA, ObB2));
console.log(compareTwoObjects(ObA, ObB3));
console.log(compareTwoObjects(ObA, ObB4));
console.log(compareTwoObjects(ObA, ObB5));
console.log(compareTwoObjects(ObA, ObB6));
console.log(compareTwoObjects(ObA, ObB7));
console.log(compareTwoObjects(ObA, ObB8));
console.log(compareTwoObjects(ObA, ObB9));
console.log(compareTwoObjects(ObA, ObB10));
console.log(compareTwoObjects(ObA, ObB11));
console.log(compareTwoObjects(ObA, ObB12));