数组去重的多重封装方法

利用for循环

Array.prototype.uniq = function(){
   var resArr = [];
   var flag = true;
   for(var i = 0; i < this.length; i++){
      if(rhis[i] != this[i]){//排除NaN
          if(flag){
             resArr.push(this[i]);
             flag = false;
          }
      }else{
          resArr.push(this[i]);
      }
   }
   return resArr;
}
Array.prototype.uniq = function() {
            var result = [];
            var pushedNaN = false;
            var pushednullobj = false;
            for (var i = 0; i < this.length; i++) {
                var index1 = this.indexOf(this[i]);
                var index2 = this.lastIndexOf(this[i]);
                if (index1 != index2) {
                    result.push(this[i]);
                    while (index1 != this.lastIndexOf(this[i])) {
                        this.splice(this.lastIndexOf(this[i]), 1);
                    }
                } else if (index1 == -1) { //针对NaN做的判断,只有第一次出现的NaN能够push进result
                    if (!pushedNaN) {
                        result.push(this[i]);
                        pushedNaN = true;
                    }
                } else if (this[i] != this[i]) { 
                //针对{}做的判断,本地测试时没起作用,且如果是两个不同的对象,则这个地方所做处理不对。
                //需要增加判断对象属性及值的机制。但是估计这个测试用例没考虑{}
                    if (!pushednullobj) {
                        result.push(this[i]);
                        pushednullobj = true;
                    }
                } else {
                    result.push(this[i]);
                }
            }
            return result;
        }
Array.prototype.uniq = function() {
            var hasNaN = false;
            for (var i = 0; i < this.length; i++) {
                if (this[i] !== this[i]) hasNaN = true;
                for (var j = i + 1; j < this.length;) {
                    if (this[i] === this[j] || (hasNaN && this[j] !== this[j])) {
                        this.splice(j, 1);
                    } else {
                        j++;
                    }
                }
            }
            return this;
}
 Array.prototype.uniq = function() {
            let arr = this,
                length = this.length,
                hash = {},
                res = []
            for (let i = 0; i < length; i++) {
                if (!hash[arr[i]] || /Object/.test(Object.prototype.toString.call(arr[i]))) {
                    res.push(arr[i])
                    hash[arr[i]] = 1
                }
            }
            return res
}
 Array.prototype.uniq = function() {
                var obj = {},
                    newArr = [],
                    item = null,
                    key = null;
                for (var i = 0; i < this.length; i++) {
                    item = this[i];
                    var type = Object.prototype.toString.call(null).replace(/\[|\]/g, "").split(" ")[1];
                    var key = type + item;
                    // key = typeof this[i] + item;
                    //过滤出Object
                    if (!obj[key] || key.indexOf("object") > -1) {
                        newArr.push(item);
                        obj[key] = true;
                    }
                }
                return newArr;
}

forEach遍历

Array.prototype.uniq = function() {
            var a = new Set();
            this.forEach((arr, i) => {
                a.add(arr);
            });
            return Array.from(a);
};

hasOwnProperty

Array.prototype.uniq = function() {
            var obj = {};
            var array = [];
            for (var i = 0; i < this.length; i++) {
                var flag = false;
                if (obj.hasOwnProperty(this[i])) flag = obj[this[i]] === this[i];
                if (flag || (!flag && obj.hasOwnProperty(this[i]) && (!this[i] === true))) continue;
                else {
                    obj[this[i]] = this[i];
                    array.push(this[i]);
                }
            }
            return array;
};

reduce累计运算

Array.prototype.uniq = function() {
            return this.reduce((prev, next) => {
                prev.findIndex(item => Object.is(item, next)) === -1 && prev.push(next);
                return prev;
            }, [])
}

filter过滤数组

Array.prototype.uniq = function() {
            return this.filter(function(ele, index, arr) {
                return index === arr.indexOf(ele);
            });
};

map数据重组

Array.prototype.uniq = function() {
            return Array.from(new Set(this.reduce((map, key) => {
                map.set(key, 1);
                return map;
            }, new Map()).keys()));
}

触发重排并进行去重

 Array.prototype.uniq = function() {
                return Object.values(this.reduce((r, c) => {
                    return {
                        ...r,
                        [c]: c
                    }
                }, {}))
}

es6

Array.prototype.uniq = function() {
            return Array.from(new Set(this));
}
Array.prototype.uniq = function() {
            return [...new Set(this)];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值