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

203
Vistas
Lodash : _Filter, but returns an array of objects with specific keys

Im making a function which compares two arrays of objects and returns two subset arrays of objects. The first returned array being a set of objects which were not included within the array passed within the second prop, and the second returned array being a set of objects which were not included within the first array but are now included within the second.

This works, however, i want to add an extra functionality to return the second array with specific keys. Is there anyway to utilise lodash "pickBy" functionality within the filter function, or will i have to create an extra recursive section?

function extract_removed_new (before, after, matching, pickBy = [])  {

let before_ = cloneDeep(before)
let new_ = _.filter(after, (item) => {
    let index_ = _.findIndex(before_, function (i) {
        return _.get(i, matching) == _.get(item, matching)
    })
    if(index_ !== -1) {
        before_.splice(index_, 1)
        return false
    }
    if(pickBy.length > 0) {
        return _.pickBy(item, function(value, key) {
            if(key in pickBy) {
                return true
            }
        });
    }
    return true
})
return [before_, new_]

}

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

0

Using lodash compact, with map I was able to get exactly what I needed.

function extract_removed_new (before, after, matching, pickBy = [])  {

let before_ = cloneDeep(before)
let new_ = _.compact(_.map(after, (item) => {
    let index_ = _.findIndex(before_, function (i) {
        return _.get(i, matching) == _.get(item, matching)
    })
    if(index_ !== -1) {
        before_.splice(index_, 1)
        return false
    }
    if(pickBy.length > 0) {
        return _.pickBy(item, function(value, key) {
            if(pickBy.includes(key)) {
                return true
            }
        });
    }
    return item
}))
return [before_, new_]

}

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