我想构建一个过滤器,该过滤器根据存在的规则数量,根据这些规则过滤对象数组。
我有一个这样的对象数组:
const myList = [
{name: 'Joe',
sex: 'male',
isStared: false,
},
{name: 'Ann',
sex: 'female',
isStared: true,
},
{name: 'Gil',
sex: 'female',
isStared: true,
},
]
我还有一个带有规则的对象,用户可以通过该规则进行过滤,例如可以是:
const rules = {sex: 'male', isStared: 'false'}
I dont want to hard code it so that it specifically checks for sex === 'male'
or isStared === true
但是我想,如果规则更少,它将检查那些规则并仅返回那些例如是男性且被盯着的规则。
我现在所拥有的是一个硬编码的过滤器,但是如果规则发生变化,它将破坏:
myList.filter(friend => friend.sex === action.filterQuery.sex && friend.sex.isStared === action.filterQuery.sex)
任何想法如何实现这一目标?
谢谢。
You can use
filter
,Object.entries
, andevery
-!
: And watch out,'false'
is not the same asfalse
. Don't wrap booleans in quotes.