mardi 26 novembre 2019

JavaScript handling multiple filter conditions

I have a big JSON information and some query strings are passed to filter that JOSN data. For example dept=eng&date=2019-11-26&passport=true. It need not be in any particular order. It can be by dept and then by date and then by passport, or only date or dept and then passport, or any possible combination.

Now, I am not asking for how to write filtering logic, but I want to know what is the best way to handle such combinations in code.

What I can think of is

if(dept){
  result = {// do something}
  if( date) {
    result = { //do something more(result }
  }
}
if(date) {
  result = { // do something }
 // handle all combinations
}

// more if conditions

I am pretty sure this is not the best approach.
Again I am not asking how to write all this filter logic but want to know what is the best way or pattern to handle such permutations and combinations of filtering.

Aucun commentaire:

Enregistrer un commentaire