JavaScript:数组中删除指定一项 arr.remove( str )

JavaScript:如何删除数组中指定的一项

Array.prototype.indexOf = function(val) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == val) return i;
        }
        return -1;
    };
//在 Array 原型中植入 remove 方法 remove 中调用 indexOf 判断 str 是否存在 arr 中,有则拿到 index
Array.prototype.remove = function(val) {
        var index = this.indexOf(val);
            if (index > -1) {
                this.splice(index, 1);
        }
    };
arr.remove('str');

将上面的代码分装起来。arr.remove() 就可直接调用了。

猜你喜欢

转载自blog.csdn.net/DOCALLEN/article/details/71515957
str
今日推荐