1

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.

Sam
  • 21
  • 2

2 Answers2

0

You could take an array of the wanted filters and check every filter for getting the resut.

const
    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' }],
    filters = [
        ({ age }) => age < 30,
        ({ shift }) => shift === 'evening'
    ],
    result = data.filter(o => filters.every(fn => fn(o)));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You could use something like this:

data.filter(x=> {
x.shift=='evening' && !x.new_employee
    if (x.name.length<5) return nameFilter(x);
    else if (x.name==='Peter' && x.age>18) return ageFilter(x);
    else if (x.name.length>10 && x.location) return locationFilter(x);
    else return defaultFilter(x);
});
Sascha
  • 4,576
  • 3
  • 13
  • 34