记录:js,去除数组中的某个元素
// 给js的数组对象定义一个函数,用于查找指定的元素在数组中的位置,即索引
Array.prototype.indexOf = function (val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val) return i;
}
return -1;
};
// 通过得到这个元素的索引,使用js数组自己固有的函数去删除这个元素
Array.prototype.remove = function (val) {
var index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
};
// 去除元素
aList.forEach(function (item) {
aList.remove(item.id);
aList.remove(item.userName);
});