Abdennour TOUMI Abdennour TOUMI - 2 years ago 111
Javascript Question

check if literal object included in other object with Built-in API

I mean Object-B is included in Object-A, when all attributes of Object-B are included in the object-A and its values are the same in the object-A.

var obj_b={a:1,d:3}
var obj_a={a:1,b:22,c:33,d:3} //--> obj_b includes in obj_a
var obj_c={a:1,f:4};

isIncluded=(small,big)=>Object.keys(small).every((k)=>big[k] === small[k])
'Does "obj-b" included in "obj-a" ? ',isIncluded(obj_b,obj_a)
'Does "obj-c" included in "obj-a" ? ',isIncluded(obj_c,obj_a)

My question is:

Does ES6/ES7 has built-in API more elegant and brief for that?

For example, to extend object from origin, ES6 brought

Is there something like

Answer Source

No, there is not. Your solution is already brief and elegant, although it could be golfed by another 4 characters.

const isSubObject = (small,big)=>Object.keys(small).every(k=>big[k]===small[k]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download