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

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

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