Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

97
Visualizações
Chaining Filters within a Loop

I currently have a collection of data called venues that contain the following fields:

const venues = [
{
name: 'test1',
categories: ['1a', '1b'],
currencies: ['2a', '2b']

},
{
name: 'test2',
categories: ['1b'],
currencies: ['2a']

}
]

The categories/currencies fields within each venue object contain the ids of the category they belong to and the currencies they accept.

I then have an array of filters that looks like the following:

const filters = [
{
name: 'art',
id: '1a',
type: 'categories'
},
{
name: 'testCurrency',
id: '2b',
type: 'currencies'
},
]

In order to chain the filters for currencies and categories I am doing the following:

 const filterVenueData = (venues, filterByType, key) => {
    const filteredVenues = venues.filter((venue) => {
      if (!filterByType[key]?.length) return true
      return filterByType[key].some((filters) => {
        return venue[key].includes(filters.id)
      })
    })

    return filteredVenues
  }



  const filtersByType = filters.reduce(
        (hash, obj) => ({
          ...hash,
          [obj['type']]: (hash[obj['type']] || []).concat(obj)
        }),
        {}
      )

const filterByCategory = filterVenueData(
        venues,
        filtersByType,
        'categories'
      )
      const filterByCurrency = filterVenueData(
        filterByCategory,
        filtersByType,
        'currencies'
      )

console.log(filterByCurrency)

while this works... I can't help but feel like having to call the function twice (and pass in the key to filter by) in order to chain the filters for both currency and categories seems very inefficient. Is there a way to chain the filters using a loop as opposed to having to call the function twice/pass in what key to filter by? I would like the filters to be strict, meaning in this current example, only venues who belong to the art category and accept testCurrency should appear (test1 venue).

I have attached a code sandbox for debugging: https://codesandbox.io/s/serverless-rain-dmluz?file=/src/index.js

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Instead of using filterVenueData for each type of filter, use the filtersByType object and iterate over its entries - check that .every one of its entries passes the test (that is, that the venue contains one of the IDs).

const venues=[{name:"test1",categories:["1a","1b"],currencies:["2a","2b"]},{name:"test2",categories:["1b"],currencies:["2a"]}],filters=[{name:"art",id:"1a",type:"categories"},{name:"testCurrency",id:"2b",type:"currencies"}];

const filtersByType = {};
for (const { id, type } of filters) {
  filtersByType[type] ??= [];
  filtersByType[type].push(id);
}
const output = venues.filter(obj =>
  Object.entries(filtersByType).every(([key, arr]) =>
    arr.some(id => obj[key].includes(id))
  )
);
console.log(output);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda