js 实现数组去重

106 阅读1分钟
  var pro = Array.prototype;
  //数组去重
  pro.myDistinct = function myDistinct(){
    var obj = {}
    for(var i=0; i<this.length;i++){
      var item = this[i]
      if(typeof obj[item] !== 'undefined'){
        this[i] = this[this.length-1]
        this.length--;
        i--;
        continue;
      }
      obj[item] = item;
    }
    obj = null;
    return this;
  }
  var arr = [1,2,3,4,5,4,3]
  arr.myDistinct();
  console.log(arr)