// 判斷對象是否在數組中
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字段作比較