Array.prototype.remove 删除数组元素
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);
};
这个函数扩展了JavaScript的内置对象Array,这样,我们以后的所有声明的数组都会自动的拥有remove能力,我们来看看这个方法的用法:
var array = ["one", "two", "three", "four", "five", "six"];
print(array);
array.remove(0);//删除第一个元素
print(array);
array.remove(-1);//删除倒数第一个元素
print(array);
array.remove(0,2);//删除数组中下标为0-2的元素(3个)
print(array);
会得到这样的结果:
one,two,three,four,five,six
two,three,four,five,six
two,three,four,five
five