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

86
Views
Filtro Lodash por objeto de objeto

¿Cómo filtrar artículos por tipo?

 const items = { 'someHash0': { type: 'foo', name: 'a' }, 'someHash1': { type: 'foo', name: 'b' }, 'someHash2': { type: 'foo', name: 'c' }, 'someHash3': { type: 'baz', name: 'd' }, };

Quiero filtrar por type=foo y obtener ese resultado:

 const items = { 'someHash0': { type: 'foo', name: 'a' }, 'someHash1': { type: 'foo', name: 'b' }, 'someHash2': { type: 'foo', name: 'c' } };

intenté con

 return _.mapValues(pairs, function (item) { return (item.type === 'foo') ?? item })

pero devuelve verdadero/falso en lugar de todo el objeto

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

0

map en general es para editar entradas iteradas, por lo que no es lo que necesita.

Puedes hacer esto:

 let result = _.filter(items, x => x.type === 'foo')

Si necesita mantener las mismas claves, puede hacerlo así:

 let result = _.pickBy(items, x => x.type === 'foo')
about 4 years ago · Juan Pablo Isaza Report

0

Un filtro sería suficiente si no necesitara volver a agregar la numeración. En ese caso, aún puede salirse con bastante facilidad combinando reduce y filter , de la siguiente manera:

 const items = { '0': { type: 'foo', name: 'a' }, '1': { type: 'foo', name: 'b' }, '2': { type: 'foo', name: 'c' }, '3': { type: 'baz', name: 'd' }, }; const mapped = _.reduce(_.filter(items, (item) => item.type === 'foo'), (acc, item, index) => { acc[index] = item return acc }, {}) console.log(mapped)
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>

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!