所以我找到了一个合理的解决方案,但它不完整:
Array.prototype.removeObjWithValue = function(name, value){
var array = $.map(this, function(v,i){
return v[name] === value ? null : v;
});
this.length = 0; //clear original array
this.push.apply(this, array); //push all elements except the one we want to delete
}
Array.prototype.removeObj = function(obj){
var array = $.map(this, function(v,i){
return v["_id"] === obj["_id"] ? null : v;
});
this.length = 0; //clear original array
this.push.apply(this, array); //push all elements except the one we want to delete
}
我仍然遇到的问题是这不起作用并继续返回 []
Array.prototype.removeObjs = function(objs){
var array = this;
console.log(array);
$.each(objs, function (i,v) {
array.removeObj(v);
console.log(array);
})
console.log(array);
this.length = 0; //clear original array
this.push.apply(this, array); //push all elements except the ones we want to delete
}