JS数组不添加重复值 & 删除指定值

JS数组的push没法判断重复值就不添加了,所以写了个这个方法

直接在数组上使用即可 

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

示例

JS数组不添加重复值 & 删除指定值

 

参考https://www.cnblogs.com/fanbi/p/9013415.html

 

 

删除指定值

    Array.prototype.removeByValue = function (val) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == val) {
                this.splice(i, 1);
                break;
            }
        }
    }

参考https://blog.csdn.net/ylhsuper/article/details/62053060