Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
Inverse function of lodash's _.invertBy()

What is the inverse function of lodash's _.invertBy() method?

I want to do a roundtrip and convert an object later to the same form after inverting it. But if I do

> _.invertBy({apple: 'fruit', pear: 'fruit'})
{ fruit: [ 'apple', 'pear' ] }
> _.invertBy({ fruit: [ 'apple', 'pear' ] })
{ 'apple,pear': [ 'fruit' ] }

and { 'apple,pear': [ 'fruit' ] } is not the same as {apple: 'fruit', pear: 'fruit'}.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

One can hack the the source code of invertBy from github and come up with something like

function invertBackBy(object, iteratee = _.identity) {
  const result = {}
  Object.keys(object).forEach((key) => {
    object[key].map(iteratee).forEach((value) => {
      result[value] = key
    })
  })
  return result
}

or if a lodash one-liner is what you wanted, I think there's nothing much simpler than

oinv => _.reduce(oinv, (acc, values, key) => 
   _.assign(acc, _.fromPairs(_.map(values, value => [value, key]))), {})

see fiddle for an example.

about 4 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda