gpt4 book ai didi

javascript - 通过两个嵌套值过滤对象

转载 作者:行者123 更新时间:2023-12-02 21:23:28 25 4
gpt4 key购买 nike

我遇到了过滤方法的问题。在我的页面上有一个按球队名称搜索比赛的输入。过滤器值被存储到 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”的所有比赛?

提前致谢!

最佳答案

尝试将条件更新为:

if (!item[key] || item[key].name !== criteria[key].name)

let filteredMatches = this.state.matches.filter(function(item) {
let flag = true;

for (let key in criteria) {

// update this to
if (!item[key] || item[key].name !== criteria[key].name)
flag = false;
}
return flag;
});

关于javascript - 通过两个嵌套值过滤对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60798733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com