我正在使用一个API,其中我获得了以下格式的数据:
[
0:{
id: "1"
name: "ttp"
platforms:{
one: "false",
}
},
1:{
id: "2"
name: "spt"
platforms:{
one: "true",
two: "true",
}
},
},
]
数据非常大,它有100多个索引。我想检查索引中是否存在平台。假设如果任何索引包含平台one
,我希望显示其id,而不希望显示没有平台one
索引。但我不知道如何在React中做到这一点。
这里的代码我想要修改,不想使用索引[0]
if (response.status === 200) {
console.log(response.data)
list = response.data[0].platforms;
}
发布于 2021-10-26 11:58:49
您可以使用filter()
来过滤数组,并根据条件获取所需的元素。
在您的案例中:
if (response.status === 200) {
console.log(response.data)
list = response.data;
const filteredList = list.filter( (e: any) => (e.platforms.one));
// filteredList contains just the objects that contains the value one
}
https://stackoverflow.com/questions/69722573
复制相似问题