Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda