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

87
Vistas
Lodash filter by object of object

How to filter items by type?

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

I want to filter by type=foo and get that result:

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

I tryied with

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

but it returns true/false instead of the whole object

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

0

map in general is for editing iterated inputs, so it's not what you need.

You can do like this:

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

If you need to keep the same keys you could do it like this :

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

0

A filter would be enough if you didn't need to re-add the numbering. In that case, you can still get away rather easily by combining reduce and filter, as follow:

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