• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

238
Vistas
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

almost 3 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

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.

almost 3 years ago · Juan Pablo Isaza Denunciar

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

almost 3 years ago · Juan Pablo Isaza Denunciar

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));
}
almost 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda