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

145
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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