1.判断某个值是否在数组中存在(扩展方法)
Array.prototype.indexOf = function (val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val) return i;
}
return -1;
};
2.移除某个已存在的值(扩展方法)
Array.prototype.remove = function (val) {
var index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
};
3.循环判断某个值是否存在于对象数组中
var equipments = app.globalData.equipments;
var bindEquipments=[];
app.httpService("/GetDevices", { userId: userId},function(result){
console.log(result)
// bindEquipments = result;
equipments.forEach(function (value, index, array) {
// 如果存在包含指定字段的设备
result.forEach(function(ralue,rindex,rarray){
if (ralue["DeviceId"] == value){
equipments.remove(value);
bindEquipments.push(ralue);
}
})
});