Find if an object is a subset of another object in javascript

I need the isSubset function, which when defining two objects compares its values ​​and indicates whether one object is a subset of the other.

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 value is different 
+5
source share
7 answers

In English:

There is some in object2 that satisfies the condition that between its value and object1 there is

Write this down:

 _.some( // Is there some key object2, // in object2 function(val, key) { // which satisfies the condition that return _.isEqual( // there is deep equality between val, // its value and object1[key] // object1? ); } ); 

In shorter form:

 _.some(object2, function(val, key) { return _.isEqual(val, object1[key]); }) 

In ES6:

 _.some(object2, (val, key) => _.isEqual(val, object1[key])) 

My suggestion is to read lodash docs and learn all the APIs, or at least the basic ones. For example, this problem will be very difficult to solve on your own if you are not aware of important procedures such as _.some and _.isEqual . On the other hand, if you know them, then this is pretty trivial.

-1
source

You can try using isSubset .

It's right

 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' }} ); 
+5
source

This can be done quite easily with lodash.

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

Using:

 const object1 = { pickUpLocation: { city: 'Hyderabad', state: 'Telangana' }} const object2 = { dist: 322, pickUpLocation: { city: 'Hyderabad', state: 'Telangana' }} isSubset(object1, object2) 
+2
source

The above answer using lodash has a limitation and does not apply to all edge case scenarios. I just came up with this solution that fits 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 
0
source

Nima's first answer cannot be right, since for the condition of the subset it must be true, all (and not just some ) elements in the "smaller" set must be contained in the "large" set. The rest is basically true, but just replace some with every and replace two objects ( big and small ):

 /** * Determine whether "small" is a subset of "big" * @see https://stackoverflow.com/questions/35737312/find-if-an-object-is-subset-of-another-object-in-javascript/48971177#48971177 */ function isSubset(big, small) { const { every, isEqual } = _; return every(small, (v, k) => isEqual(v, big[k]) ); } // test it! var object1 = { pickUpLocation : {city : 'Hyderabad', state: 'Telangana' }}; var object2 = { dist : 322, pickUpLocation: {city : 'Hyderabad', state: 'Telangana' }}; var a = {i:1, j:2, k:3}; var b = {i:1, j:2}; var c = {i:2, j:2}; console.log([ isSubset(a, b), isSubset(a, c), isSubset(b, a), isSubset(object1, object2), isSubset(object2, object1) ]); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.js"></script> 

PS: Here is another way to do this, but it is probably slower as it copies the material and does not stop earlier:

 function isSubset(big, small) { const { pick, isEqual } = _; return isEqual(pick(big, Object.keys(small)), small); } 
0
source

Here is a simple solution:

  import _ from "lodash" // is a subset of b? function isSubset(a, b, merge = false) { const assign = merge ? _.merge : _.assign; var c = assign(_.cloneDeep(b), a); return _.isEqual(b, c); } 
0
source
 function isSubset(obj1, obj2) { for (var key in obj2){ if (JSON.stringify(obj2[key]) === JSON.stringify(obj1[key])) return true; } return false; } 

Edit: now general, but if you want it to be more general, you should see a comparison link fooobar.com/questions/2676 / ...

-1
source

Source: https://habr.com/ru/post/1244267/


All Articles