Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

296
Views
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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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));
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!