Here is my sample code
data = [
{name: "Sam", age: 25, location: 'deleware', new_employee: true, shift: 'day'},
{name: "John", age: 45, location: 'new york', new_employee: false, shift: 'day'},
{name: "Tony", age: 65, location: 'california', new_employee: true, shift: 'evening'},
{name: "Bruce", age: 23, location: 'california', new_employee: true, shift: 'evening'},
{name: "Tom", age: 34, location: 'texas', new_employee: false, shift: 'day'},
{name: "Rob", age: 51, location: 'ohio', new_employee: false, shift: 'day'},
{name: "Jacob", age: 21, location: 'ohio', new_employee: false, shift: 'evening'},
{name: "Steve", age: 38, location: 'deleware', new_employee: false, shift: 'day'}
]
If I want to filter this I can do something like to filter all evening shifts:
data.filter(x=> x.shift=='evening') // gives me the filtered list
If I want to filter more than one condition I can do something like this
to filter our evening shift and not new emp
data.filter(x=> x.shift=='evening' && !x.new_employee) //gives me the exact one
Now the challenge I face is, I do not know what all filters the user will pass. I want to build this filter dynamically. Currently I am achieving this using multiple if else statements for various possible combination. combination include
if name then that filter
else if name and age then another filter
else if name location then another filter
else if name and age and location another filter
and so on
I know there is definitely a better approach to do this. Any suggestion or point to the right direction will be very helpful.