我正在使用一个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;
}
您可以使用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
}