1
let a = { first: "Tybalt", last: "Capulet" }

let b = {last: "Capulet" }

How do I check if 'a' contains 'b'?

olafsadventures
  • 151
  • 1
  • 10
  • 1
    Possible duplicate of [How to check if an object contains another object?](https://stackoverflow.com/questions/46211122/how-to-check-if-an-object-contains-another-object) – sadrzadehsina Dec 23 '18 at 03:28
  • Possible duplicate of [Object Comparing: check if an object contains the whole other object](https://stackoverflow.com/questions/23045652/object-comparing-check-if-an-object-contains-the-whole-other-object) – ellipsis Jan 12 '19 at 20:29

5 Answers5

1

I think there are two ways to obtain what you desire

1st-> like Jack Bashford has done with his answer

2nd -> use rest operators to make a new object and compare object with itself

let a = { first: "Tybalt", last: "Capulet" }

let b = {last: "Capulet" }
let combinedObj = {...a, ...b}

if a contains b in it, the combinedObj will be equal to a. Then you can use any object comparison module to check if combinedObj and a are equal or not.

Prasanna
  • 4,125
  • 18
  • 41
1

Try This :

let a = { first: "Tybalt", last: "Capulet", age: 20 }
let b = { last: "Capulet", first:"Tybalt" }
var result = false ;

function getLength(obj) {
  var len = 0 ;
  for( key in obj ) 
    len++ ;
  return len ;
}

if ( getLength(a) >= getLength(b) ) {
  for ( key in b ) {
    if (b[key] !== a[key] ) {
      result = false ;
      break ;
    }
    result = true ;
  }
}

console.log( result ) ;
Ehsan
  • 12,655
  • 3
  • 25
  • 44
  • Why not JSON.stringify for converting to string? – quirimmo Dec 23 '18 at 04:33
  • This fails when the property order isn't the same. For example if `b` is `{last: "Capulet", first: "Tybalt" }` – Mark Dec 23 '18 at 04:36
  • Hmm…interesting. Maybe if you have some sort of separator between key and value that will never show up. Because otherwise you have edge cases like `let b = { lastC: "apulet", first:"Tybalt" }` which pass. – Mark Dec 23 '18 at 05:16
0

Iterate through each property in the smaller object, then if there are any that don't match, return false. Otherwise, return true:

let a = { first: "Tybalt", last: "Capulet" };

let b = {last: "Capulet" };

function bigObjContainsSmallObj(big, small) {
    for (var prop in small) {
        if (small[prop] != big[prop]) {
            return false;
        }
    }
    return true;
}

Demonstration:

let a = {
  first: "Tybalt",
  last: "Capulet"
};

let b = {
  last: "Capulet"
};

let c = {
  first: "Romeo"
}

function bigObjContainsSmallObj(big, small) {
    for (var prop in small) {
        if (small[prop] != big[prop]) {
            return false;
        }
    }
    return true;
}

console.log(bigObjContainsSmallObj(a, b));
console.log(bigObjContainsSmallObj(a, c));
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • `small[prop] != big[prop]` this may not work if the values are object (array, json). – Prasanna Dec 23 '18 at 03:53
  • 1
    Since you’re returning from inside the loop, this won’t work if small object has more than 1 prop. – Derek Nguyen Dec 23 '18 at 03:57
  • @DerekNguyen it will only return if the values aren't equal which means the properties don't match. If a property doesn't match there's no reason to continue looping. – Mark Dec 23 '18 at 04:24
  • @MarkMeyer Ah true, my bad Jack, I was thinking of looking for common properties, which is not what OP's question about – Derek Nguyen Dec 23 '18 at 04:35
0

This should work.

function checkObj(obj, obj2, property){
    if( obj.hasOwnProperty( property ) ) {
        console.log('Yes. It has the '+ property);
        if(obj[property] === obj2[property]){
            console.log('And the values are both '+ obj2[property]);
        }
    }
}

checkObj(a, b, 'last');
andre mcgruder
  • 1,120
  • 1
  • 9
  • 12
0

You can iterate through each keys of b object and check if its value is equal to value of that key in a object.

let a = { first: "Tybalt", last: "Capulet" },
    b = { last: "Capulet" },
    c = { here: 'property'},
    isObjectPresent = (a, b) => {
      return Object.keys(b).every(k => k in a && a[k] === b[k]); 
    }
console.log(isObjectPresent(a,b));
console.log(isObjectPresent(a,c))
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51