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

148
Views
How to filter array of objects by different properties based on array that declare what property to check?

thank you for help. I have an array of objects. And i can't find good solution except of a lot of IF's how to filter it depends on array of properties that should be checked.

const offers = [
  {
    isGrouped: true,
    isDiscarded: false,
    isCopy: false,
    isWarned: false,
    name: "qw",
  },
  {
    isGrouped: false,
    isDiscarded: true,
    isCopy: false,
    isWarned: false,
    name: "qwer",
  },
  {
    isGrouped: false,
    isDiscarded: true,
    isCopy: true,
    isWarned: false,
    name: "erw",
  },
  {
    isGrouped: false,
    isDiscarded: false,
    isCopy: true,
    isWarned: false,
    name: "frew",
  }]

and here is the array of filters

export enum Status {
  DISCARDED = "Discarded",
  WARNED = "Warning",
  COPY = "Copy",
  GROUPED = "Grouped",
}

const filters: string[] = [Status.DISCARDED, Status.COPY];

filters could include all four statuses or one, two, three or any of them.

function check(offer, filters) {
    // I don't know actually how to handle filtering here.
    // only one idea is to check all possible variants ex.
    
    // if (filters.includes(Status.DISCARDED) && filters.length === 1) {
    //     return offer.isDiscarded
    // } else if (filters.includes(Status.DISCARDED) && filters.includes(Status.COPY) && filters.length === 2) {
    //     return offer.isDiscarded || offer.isCopy
    // }
}

offers.filter(offer => check(offer, filters))

My solutuion doesn't looks nice. Could someone take a look please?

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

0

type Offer = { [K in Status as `is${K}`]?: boolean }

function check(offer: Offer, filterStatus: Status[]): boolean {
    for (const status of filterStatus) {
        const propertyName = "is"+status as `is${typeof status}`
        if (!offer[propertyName]) return false
    }
    return true
}
about 4 years ago · Juan Pablo Isaza Report

0

I would layer this atop a more general-purpose function that tests whether a collection of key/value pairs matches your object. We can layer on something which maps Discarded to isDiscarded, etc.

const checkProps = (entries) => (x) =>
  entries .every (([k, v]) => x [k] == v)

const check = (filters) => 
  checkProps (filters .map (k => [`is${k}`, true]))

const findMatches = (offers, filters) =>
  offers .filter (check (filters))


const offers = [{isGrouped: true, isDiscarded: false, isCopy: false, isWarned: false, name: "qw"}, {isGrouped: false, isDiscarded: true, isCopy: false, isWarned: false, name: "qwer",}, {isGrouped: false, isDiscarded: true, isCopy: true, isWarned: false, name: "erw", }, {isGrouped: false, isDiscarded: false, isCopy: true, isWarned: false, name: "frew"}]
const Status = {DISCARDED: "Discarded", WARNED: "Warning", COPY: "Copy", GROUPED: "Grouped"}


console .log (findMatches (offers, [Status.DISCARDED, Status.COPY]))

checkProps takes an array of key-value pairs and returns a function which accepts an object, reporting whether all those keys in the object match the relevant value. check takes your filters and turns them into such an array of key-value pairs by prepending "is" to the filter name and pairing it up with the value true. We could use this by directly calling

offers .filter (check (filters))

but I find it cleaner to wrap that in the function findMatches

This offers a fairly flexible API, but we can probably get even more flexible without making it much harder to use. Imagine this:

students .filter (where ({
  gpa: gte (3.0),
  grade: eq (11),
  gender: eq ('male'),
  demerits: lt (3)
}))

It would be relatively easy to build an API that worked like that on very similar principles to the above, and it would be straightforward to layer your function on top of this. I wont try to write it here. But this is a demonstration of a useful point: it often helps simplify your code to think of it more generically and then add your special case on top of the generic abstraction.

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!