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

106
Views
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 answers
Answer question

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