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

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

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