简体   繁体   中英

Check if object has only the given keys using Lodash or Underscore

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

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.

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

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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM