DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> JavaScript數組去重的3種方法和代碼實例
JavaScript數組去重的3種方法和代碼實例
編輯:關於JavaScript      日期:2016/12/23 17:29:09

數組去重的方法有很多,到底哪種是最理想的,自己不清楚。於是自己測試了下數組去重的效果和性能。測試十萬個數據,代碼和所耗大概時間如下。

  到底采用哪種方法,根據實際情況而定吧。

/*方法一: 1,'1' 會被認為是相同的; 所有hash對象,如:{x;1},{y:1}會被認為是相同的 //10ms */
Array.prototype.unique=function(){
  var newArr=[],obj={};
  for(var i=0,len=this.length;i<len;i++){
    if(!obj[this[i]]){ 
      newArr.push(this[i]);
      obj[this[i]]=true;
    }
  }
  return newArr;
}

/*方法一改進版:所有hash對象,如:{x;1},{y:1}會被認為是相同的 //30ms*/
Array.prototype.unique=function(){
  var newArr=[],obj={};
  for(var i=0,len=this.length;i<len;i++){
    if(!obj[typeof(this[i])+this[i]]){ 
      newArr.push(this[i]);
      obj[typeof(this[i])+this[i]]=this[i];
    }
  }
  return newArr;
}

/*方法二: 去重結果最好,但耗性能   //250ms*/
Array.prototype.unique=function(){
  var newArr=this.concat();
  for(var i=0,len=newArr.length;i<len;i++) {
    for(var j=i+1,len=newArr.length;j<len;j++) {
      //注意 ===
      if(newArr[i]===newArr[j]) {
        newArr.splice(j,1);
        j--;
      }
    }
  }
  return newArr;
}

/*方法三: 不能去重hash對象 //25ms */
Array.prototype.unique = function(){
  var newArr = []; //一個新的臨時數組
  for(var i = 0,len=this.length; i < len; i++){    
    if (newArr.indexOf(this[i]) == -1){  //如果當前數組的第i已經保存進了臨時數組,那麼跳過,否則把當前項push到臨時數組裡面
      newArr.push(this[i]);
    }
  }
  return newArr;
}


var arr0=[11,21,221,13,24,"134","1",{x:1,y:1},{name:"pobaby",age:"12",hobby:"football"},{name:"pobaby1",age:"121",hobby:"football1"},{x:134},{y:132},{x:143},{y:3421},"神秘人物", "火柴人技巧格斗", "超音速戰場", "小小辛打磚塊", "火柴人技巧格斗", "加菲貓超人", "小小辛打磚塊", "卑鄙的我2", "電流導線", "飛天手推車","神D秘人物", "火柴人S技巧格斗", "超音SD速戰場", "小小SD辛打磚塊", "火柴人SD技巧格斗", "加菲S貓超人", "小小DF辛打磚塊", "卑鄙的FS我2", "電D流導線", "飛天SD手推車","神秘SD人物", "火柴人技D巧格斗", "超音ASD速戰場", "小小辛打SAD磚塊", "火柴人技SD巧格斗", "加菲FDS貓超人", "小小辛打SDF磚塊", "卑鄙SDF的我2", "電流SDF導線", "飛天手DF推車","神秘SD人物", "火柴人技AS巧格斗", "超音速戰FS場", "小小辛SDF打磚塊", "火柴人SDF技巧格斗", "加菲SD貓超人",113,231,2221,123,234,"1334","21",{x:13,y:132},{name:"pobaby2",age:"122",hobby:"football2"},{name:"pobaby13",age:"1231",hobby:"football41"},{x:13544},{y:1352},{x:14543},{y:34521},"神秘人sd物", "火柴人技sd巧格斗", "超音速sd戰場", "小小辛sd打磚塊", "火柴人技巧gw格斗", "加菲貓ui超人", "小小辛yi打磚塊", "卑鄙的yi我2", "電流yt導線", "飛天手ytui推車","神Dyu秘人物", "火yui柴人S技yui巧格斗", "超音SDyu速戰場", "小小SD辛打磚uyi塊", "火柴yui人SD技巧格斗", "加yui菲S貓超人", "小小DF辛打磚ui塊", "卑鄙uyi的FS我2", "電D流導yui線", "飛天SD手推uyi車","神i秘SD人物", "火柴人技Dhk巧格斗", "超音ASD速戰hk場", "小小辛打SAhkD磚塊", "火柴人技SD巧ghk格斗", "加菲FDS貓k超人", "小小辛打SDF磚ytui塊", "卑鄙SDF的yui我2", "電流SDyuF導線", "飛天手yuiDF推車","神iy秘SD人hk物", "火柴uyi人技AS巧格hk斗", "超音hg速戰FS場", "小小辛SDF打磚hjk塊", "火柴人SDF技hj巧格斗", "加菲SDhk貓超人" ];

/*十萬個隨機數據*/
var arr=[],num;
for(var i = 0; i < 100000; i++){
  num=Math.floor(Math.random()*50);
  arr.push(arr0[num]);
}


var t1= new Date().getTime(); console.log(t1); //開始時間

arr.unique(); //去重

var t2 = new Date().getTime(); console.log(t2); //結束時間

console.log(t2-t1);

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved