判斷json對象是否在數組中



// 判斷對象是否在數組中
function objinArrar(check,param){
var isExisted = false;
var index = -1;
for(var i=0;i<param.length;i++){
if (param[i].id == check.id) {
isExisted = true;
index = i;
return i;
}
}
if (!isExisted) {
console.log ("choose error");
return -1;
}else{
return index;
}
}

如果在數組中會返回數組中的索引,如果不在返回 -1;

id一般可能作息數組中對象的唯一標識,所以以id字段作比較


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM