0

How can i compare 2 objects and their values using ES6?

Objects i have:

let obj1 =  { c: 1, o: 1, m: 2, a: 1, s: 1 }
let obj2 =  { a: 4, b: 2, c: 3, m: 2, o: 1, s: 1, d: 2 }
   

I want to check if obj1[key] exists in obj2. And if value of this key in obj2 same as in obj1 return true;

PS i'm kinda new to programming

  • 1
    You probably meant to define equals as: "both object have the exact same keys, and to the same keys are mapped the exact same values", not just "one side" of the equation... – Tal Mar 14 '21 at 18:28

3 Answers3

0

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!

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • The question is about checking if all the keys of source obj are in destination are not, not checking the strict equality. In the example in question, we have to check if all the keys in `obj1` are in `obj2` and corresponding values are equal – Shankar Regmi Mar 14 '21 at 18:14
  • This will return `false` for different object size. – Anonymous Zombie Mar 14 '21 at 18:15
  • @AnonymousZombie and Shankar - did you read the first two lines of the answer??? – Nir Alfasi Mar 14 '21 at 18:22
  • @ShankarRegmi the question is: "How can i compare 2 objects and their values using ES6?". Then the OP fails to define correctly what is "equal". But the intention is clear! – Nir Alfasi Mar 14 '21 at 18:33
  • @NirAlfasi Did the Question mentioned anything about the object size? Don't assume by yourself. False Assumption – Anonymous Zombie Mar 14 '21 at 18:41
  • @AnonymousZombie do you know what is the definition of object-equality? – Nir Alfasi Mar 14 '21 at 18:45
0

I made a simple mockup with "pseudocode" comment that should help you understand

function checkObjInTables(tab1, tab2, key){
    // return {KEY EXIST IN TABLE 1} AND {KEY EXIST IN TABLE 2} AND {VALUE OF KEY IN TABLE 1} IS EQUAL TO {VALUE OF KEY IN TABLE 2}
    return key in tab1 && key in tab2 && tab1[key] == tab2[key];
};

let obj1 =  { c: 1, o: 1, m: 2, a: 1, s: 1 }
let obj2 =  { a: 4, b: 2, c: 3, m: 2, o: 1, s: 1, d: 2 }

console.log(checkObjInTables(obj1, obj2, "s")); // true
console.log(checkObjInTables(obj1, obj2, "a")); // false
aze
  • 11
  • 2
-1

There is a JavaScript way.

let obj1 = { c: 1, o: 1, m: 2, a: 1, s: 1 };
let obj2 = { a: 4, b: 2, c: 3, m: 2, o: 1, s: 1, d: 2 };

const isObject = obj => typeof obj === 'object' && obj !== null;

function check(obj1, obj2) {
    // check if both inputs are object and not null
    if(!isObject(obj1) || !isObject(obj2)) return false;
    
    // check if both obj has equal size
    if(Object.keys(obj1) !== Object.keys(obj2)) return false;
    
    for (const key of Object.keys(obj1)) {
        if (!(key in obj2) || obj1[key] !== obj2[key]) return false;
    }
    return true;
}

console.log(check(obj1, obj2));
Shankar Regmi
  • 854
  • 1
  • 7
  • 16
  • I'm afraid that people will copy this answer bc of the upvotes. This is bad. The OP asked for "equals" but didn't give a proper definition of "equals" and this answer "abuses" it. I don't think that `o1 = {a:1}; o2={a:1, b:2}` should be considered as `equals` while this answer DOES treat them as such. – Nir Alfasi Mar 14 '21 at 18:24
  • 1
    @NirAlfasi. Thanks, I have modified the answer. – Shankar Regmi Mar 14 '21 at 18:37