【发布时间】:2020-07-03 00:59:06
【问题描述】:
我遇到了过滤方法的问题。在我的页面上有一个按团队名称搜索匹配项的输入。过滤器值被存储到 React 状态。匹配对象如下所示:
[
{
"id": 4,
"teamBlue": {
"id": 36,
"name": "nameForTeamBlue",
"playerList": [
{
[...]
}
]
},
"teamRed": {
"id": 37,
"name": "nameForTeamRed",
"playerList": [
{
[...]
}
]
},
"localDate": "2020-01-01",
"localTime": "00:00:00",
"referee": null,
"commentator1": null,
"commentator2": null,
"streamer": null,
"stage": {
"id": 2,
"name": "GROUPSTAGE"
},
"onLive": true,
"finished": false
},
]
我尝试了很多方法来按团队名称过滤比赛,例如:
let criteria = {
teamBlue: {
name: this.state.filter
},
teamRed: {
name: this.state.filter
}
};
let filteredMatches = this.state.matches.filter(function(item) {
for (let key in criteria) {
if (item[key] === undefined || item[key] !== criteria[key])
return false;
}
return true;
});
console.log(filteredMatches);
但他们都没有工作。 有没有办法过滤这些匹配项,所以当我在输入中输入“blue”时,它会显示所有团队名称包含“blue”的匹配项?
提前致谢!
【问题讨论】:
-
您只按名称过滤,或者如果条件有键
id,那么您也想按 id 过滤?
标签: javascript arrays reactjs sorting filtering