I want to build a filter that depending on how many rules there are, it filters an arrya of objects based on those rules.
I have an array of objects like this:
const myList = [
{name: 'Joe',
sex: 'male',
isStared: false,
},
{name: 'Ann',
sex: 'female',
isStared: true,
},
{name: 'Gil',
sex: 'female',
isStared: true,
},
]
I also have an object with the rules to fillter by which the user specifies, for example it can be:
const rules = {sex: 'male', isStared: 'false'}
I dont want to hard code it so that it specifically checks for sex === 'male'
or isStared === true
But I want, that if there are more are less rules, it checks for those and returns only those that for example are male and are stared.
What i have right now is a hard coded filtering, but if the rules change, it will break:
myList.filter(friend => friend.sex === action.filterQuery.sex && friend.sex.isStared === action.filterQuery.sex)
Any idea how to achieve this?
Thanks.