Find if an object is subset of another object in javascript

12,524

Solution 1

Using Lodash isMatch

_.isMatch({prop: 'object', id: 3}, {prop: 'object'})

Performs a partial deep comparison between object and source to determine if object contains equivalent property values.

Solution 2

It's a bit late, but it might help someone looking for answer without using any external library.

isSubset = (superObj, subObj) => {
    return Object.keys(subObj).every(ele => {
        if (typeof subObj[ele] == 'object') {
            return isSubset(superObj[ele], subObj[ele]);
        }
        return subObj[ele] === superObj[ele]
    });
};

let object1 = { pickUpLocation : {city : 'Hyderabad', state: 'Telangana' }};
let object2 = { dist : 322, pickUpLocation:  {city : 'Hyderabad', state: 'Telangana' }};
console.log(isSubset(object2, object1));

let object3 = { pickUpLocation : {city : 'Chennai', state: 'Telangana' }}
let object4 = { dist : 322, pickUpLocation: {city : 'Hyderabad', state: 'Telangana' }}
console.log(isSubset(object4, object3));

Solution 3

You can try to use isSubset package.

This is true

isSubset(
  { dist : 322, pickUpLocation:  {city : 'Hyderabad', state: 'Telangana' }},
  { pickUpLocation : {city : 'Hyderabad', state: 'Telangana' }}
);

This is false

isSubset(
  { dist : 322, pickUpLocation:  {city : 'Hyderabad', state: 'Telangana' }},
  { pickUpLocation : {city : 'Chennai', state: 'Telangana' }}
);

Solution 4

It can be done pretty easily with lodash.

import _ from 'lodash'

const isSubset = (aSubset, aSuperset) => (
    _.every(aSubset, (val, key) => _.isEqual(val, aSuperset[key]))
)

Usage:

const object1 = { pickUpLocation: { city: 'Hyderabad', state: 'Telangana' }}
const object2 = { dist: 322, pickUpLocation:  { city: 'Hyderabad', state: 'Telangana' }}

isSubset(object1, object2)

Solution 5

The above answer using lodash has limitation and doesn't cover all edge case scenarios. I just came up with this solution that matches all scenarios

import _ from 'lodash';

isSubset(obj1, obj2) {
  let matched = true;
  _.forEach(obj1, (value, key) => {
    if(!requirements || !_.isEqual(value, obj2[key])) {
      matched = false;
      return;
    }
  });
  return matched;
}

Case 1:

const obj1 = { foo: 'bar' };
const obj2 = { foo: 'bar', baz: 'qux' };
console.log(isSubset(obj1, obj2)); // true

Case 2:

const obj1 = { foo: 'bar' };
const obj2 = { foo: 'bar' };
console.log(isSubset(obj1, obj2)); // true

Case 3:

const obj1 = { foo: 'bar', baz: 'qux' };
const obj2 = { foo: 'bar'};
console.log(isSubset(obj1, obj2)); // false

Case 4:

const obj1 = undefiend;
const obj2 = undefiend;
console.log(isSubset(obj1, obj2)); // true

Case 5:

const obj1 = undefiend;
const obj2 = { foo: 'bar'};
console.log(isSubset(obj1, obj2)); // true

Case 6:

const obj1 = { foo: 'bar'};
const obj2 = undefiend;
console.log(isSubset(obj1, obj2)); // false
Share:
12,524

Related videos on Youtube

kkrishnaai
Author by

kkrishnaai

Updated on October 31, 2022

Comments

  • kkrishnaai
    kkrishnaai over 1 year

    I need a function isSubset, which when given two objects compares its values and tell if one object is subset of another.

    object1 = { pickUpLocation : {city : 'Hyderabad', state: 'Telangana' }};
    object2 = { dist : 322, pickUpLocation:  {city : 'Hyderabad', state: 'Telangana' }};
    
    isSubset(object1, object2); //should return true
    
    object3 = { pickUpLocation : {city : 'Chennai', state: 'Telangana' }}
    object4 = { dist : 322, pickUpLocation: {city : 'Hyderabad', state: 'Telangana' }}
    
    isSubset(object3, object4) //should return false as city's value is different
    
    • Admin
      Admin about 8 years
      This question may show a lack of effort or research, which is a perfectly good reason to down-vote it. However, it does not satisfy the criteria for close-voting due to seeking debugging help yet not including the minimal example etc., since it's not seeking debugging help in the first place. Close-voters, please exercise your votes carefully and in accordance with the stated criteria.
    • Bondolin
      Bondolin about 5 years
      shrugs It's a request for information. Helps me. +1
  • payne
    payne over 2 years
    Note: null is seen as an actual value. This means if you replace {prop: 'object'} with {prop: 'object', id: null}, the function will return false.
  • sbocinec
    sbocinec over 2 years
    3 of your examples contain typo: undefiend -> undefined