以下代碼獲取特定 ID 的索引作為回應。當 ID 是唯一的時,它作業正常。但是現在我有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/334698.html
標籤:javascript 数组 反应原生 索引
