JavaScript的prototype实现移除数组元素

作者:陆金龙    发表时间:2015-07-12 16:29   


//对原数组操作,移除元素
Array.prototype.remove = function (item) {
    var n = 0;
    for (var i = 0; i < this.length; i++) {
        if (this[i] != item) {
            this[n++] = this[i];
        }
    }
    this.length -= 1;
}  

//移除指定元素,返回新的数组
Array.prototype.remove = function (obj) {
    var a = [];
    for (var i = 0; i < this.length; i++) {
        if (this[i] != obj) {
            a.push(this[i]);
        }
    }
    return a;
}