Suppose we have 2 objects:
var foo = {
a: 3,
b: 'yellow',
c: {
d: 5,
e: 'green'
}
},
bar = {
b: 'yellow',
c: {
d: 5,
e: 'green'
}
};
What is the best way to check that foo contains bar?
I am already using Lodash in my application, so you can use functions from that library if it makes it easier. Otherwise, vanilla JS is fine.
[EDIT]
Well, let me try to explain what I mean when I use term 'contain'. I will do it in LoDash terminology.
LoDash has _.extend method, so that if have 2 objects:
var obj1 = {a:1, b:2},
obj2 = {b:4, c:{d:'green', e:5}}
then the operation _.extend(obj1, obj2) will 'extend' obj1 - it will now have the value
{a:1, b:4, c:{d:'green', e:5}}
i.e. obj1 'contains' obj2 now.
_.isMatch(foo, bar) does the job.
https://lodash.com/docs/4.17.15#isMatch
@GregL gave a working solution for the question. It works for me. But I found similar more concise solution, so I decided to reveal it:
_.some([foo], bar)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With