我正在使用一個 API 來獲取這種格式的資料:-
[
0:{
id: "1"
name: "ttp"
platforms:{
one: "false",
}
},
1:{
id: "2"
name: "spt"
platforms:{
one: "true",
two: "true",
}
},
},
]
資料非常大,有100多個索引。我想檢查索引中是否存在平臺。假設如果任何索引包含 platformone我想顯示它的 id 并且不想顯示沒有 platform 的索引one。但我是如何在 React 中做到這一點的。
這里是我想改的代碼,不想用索引 [0]
if (response.status === 200) {
console.log(response.data)
list = response.data[0].platforms;
}
uj5u.com熱心網友回復:
您可以使用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://www.uj5u.com/qiye/339008.html
標籤:javascript 数组 反应 接口
