日度归档:2018-12-07

js删除数组里的某个元素

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};
// 移除数组中的第二项
array.remove(1);
// 移除数组中的倒数第二项
array.remove(-2);
// 移除数组中的第二项和第三项(从第二项开始,删除2个元素)
array.remove(1,2);
// 移除数组中的最后一项和倒数第二项(数组中的最后两项)
array.remove(-2,-1);

来自 jQuery 之父的 stackoverflow