javascript删除数组某个元素

1.首先可以给js的数组对象定义一个函数,用于查找指定的元素在数组中的位置,即索引

Array.prototype.indexOf = function(val) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == val) {
      return i;
    }
  }
  return -1;
}

2.然后使用通过得到这个元素的索引,使用js数组自己固有的函数去删除这个元素
Array.prototype.remove = function(val) {
  var index = this.indexOf(val);
  if (index > -1) {
    this.splice(index, 1);
  }
}

原文地址:https://www.cnblogs.com/michaelShao/p/5547413.html