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

116
Visualizações
filter in react with multiple conditions

I am applying following filter on a table with the help if useEffect

useEffect(()=>{
let updated = data.filter(function(e){
if ((e.country === aaa|| e.country === undefined) && (e.age === bbb|| e.age === undefined)){
  return true
}
})
  setRows(updated)
},[aaa, bbb])

it works fine when "Select" for both the filters are selected if one if them is undefined then it does not show any data

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

0

I prefer making multiple functions when filtering criteria differs. It's more readable for other developers and easier to modify and maintain.

I don't know about the actual end result since I don't know your exact use case. But this structure of code should give you a hint on how to approach that:

const byCountry = (country) => (item) => {
  return item.country === country || item.country === undefined;
};

const byAge = (age) => (item) => {
  return item.age === age || item.age === undefined;
};

const getUpdated = (data, country, age) => {
  return data
    .filter(byCountry(country))
    .filter(byAge(age));
};


const data = [{
  country: 'Finland',
  age: '42',
}, {
  country: 'Sweden',
  age: '39',
}, {
  country: 'Sweden',
}];



console.log(getUpdated(data, 'Finland', '42'));
console.log(getUpdated(data, 'Sweden', '39'));

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