我有以下代碼來獲取特定 Id 的索引作為回應。當 Id 僅為 1 時,此代碼作業正常。但是現在我有 3 個 ID。那么如何獲取多個 ID 的多個索引
function getIndex(CategoryID) {
return response.responseContents.findIndex(
(obj) => obj.CategoryID === CategoryID,
);
}
const index = getIndex(CategoryToGetName);
uj5u.com熱心網友回復:
您可以執行一個簡單的 for 回圈,在其中使用匹配的索引填充陣列。
let idxs = [];
for (let i in response.responseContents) {
if (response.responseContents[i] == CategoryID) idxs.push(parseInt(i));
}
return idxs;
uj5u.com熱心網友回復:
你可以這樣做:
function getIndex(CategoryID)
{
return response.responseContents.map((obj, index) => {
if (obj.CategoryID === CategoryID)
{
return index;
}
}
}
const index = getIndex(CategoryToGetName);
uj5u.com熱心網友回復:
請嘗試以下代碼
findIndex(CategoryIDS){
return response.responseContents.filter((r,index)=>
if(CategoryIDS.indexOf(r.CategoryID)>-1){
return index;
}
)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/330950.html
標籤:javascript 数组 反应原生 索引
上一篇:如何在不覆寫的情況下填充陣列
