js实现数组去重

<script>

  Array.prototype.myUnique = function(){
    var obj = {};
    for(var i = 0; i < this.length; i++){
      var cur = this[i];
      if(obj[cur] == cur){
        this[i] = this[this.length-1];
        this.length--;
        i--;
        continue;
      }
      obj[cur] = cur;
    }
    obj = null;
    return this;
  }

  var arr = [1,2,6,5,7,5,6];
  // 链式写法:执行数组的一个方法紧接着执行另外一个方法
  arr.myUnique().sort(function(a,b){return a-b;});
  console.log(arr);
  </script>

posted on 2018-03-27 22:56  ywx354980  阅读(82)  评论(0编辑  收藏  举报