数组去重方法

时间:2022-10-25 18:59:53
Array.prototype.distinct = function (){
  var arr = this,
  i,
  obj = {},
  result = [],
  len = arr.length;
    for(i = 0; i< arr.length; i++){
        if(!obj[arr[i]]){ //如果能查找到,证明数组元素重复了
         obj[arr[i]] = 1;
         result.push(arr[i]);
        }
    }
  return result;

  };


//例:

var p = [11,22,33,33,44]

var q  = p.distinct();

console(q)//[11,22,33,44]