Reputation: 10247
Is there a Lodash or Underscore method which can find if an object has only the given keys of that object. I would like a Lodash or Underscore implementation even though this sounds trivial using native JS.
For example if my object looks like and assuming there is a lodash method named hasOnly
const obj = {
name: undefined,
age: 15,
school: 'Some school'
}
_.hasOnly(obj,['name','age']) //return false
_.hasOnly(obj,['name','age','city']) //return false
_.hasOnly(obj,['name','age','school']) //return true
I couldn't seem to find a way in the docs
Upvotes: 0
Views: 62893
Reputation: 4366
I think Enlico's answer is fine, but for completeness I'll mention another option which doesn't require sorting. This is based on comparing objects directy instead of comparing arrays of keys.
Note that the code below assumes the original Underscore. For Lodash, replace _.mapObject
by _.mapValues
.
// replace all properties by true to prevent costly recursion
const mask = obj => _.mapObject(obj, _.constant(true));
function hasOnly(obj, keys) {
const masked = mask(obj);
// compare obj to a trimmed version of itself
return _.isEqual(masked, _.pick(masked, keys));
}
Upvotes: 1
Reputation: 28406
Quick and dirty:
hasOnly = (obj, props) => _.isEqual(_.keys(obj).sort(), props.sort())
The sorting is done because we are comparing arrays.
As an alternative, one could turn both props
and _.keys(obj)
into objects where the props
and _.keys(obj)
are the keys, whereas the value is a dummy one, always the same, such as 1
. The function to do so could be something like this:
make1ValuedObj = keys => _.zipObject(keys, Array(keys.length).fill(1))
Then one would pass those to _.isEqual
without having to sort anything:
hasOnly = (obj, props) => _.isEqual(make1ValuedObj(_.keys(obj)), make1ValuedObj(props))
The reality is that a kind of "sorting" has to happen when you construct the objects, so I don't think there's a real advantage over the solution above.
Upvotes: 4
Reputation: 136094
The native solution will be faster in almost all cases:
const obj = {
name: undefined,
age: 15,
school: 'Some school'
}
const hasOnly = (obj,props) => {
var objProps = Object.keys(obj)
return objProps.length == props.length && props.every(p => objProps.includes(p))
}
console.log(hasOnly(obj,['name','age'])) //return false
console.log(hasOnly(obj,['name','age','city'])) //return false
console.log(hasOnly(obj,['name','age','school'])) //return true
Benchmarking this against the other answer using lodash shows the lodash solution to be 95% slower (on my machine)
Benchmarks: https://jsbench.me/r9kz2mwr9c/1
Upvotes: 2