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

105
Views
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 answers
Answer question

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 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!