溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

JavaScript中數(shù)組去重的示例分析

發(fā)布時(shí)間:2021-08-07 11:05:06 來源:億速云 閱讀:115 作者:小新 欄目:web開發(fā)

這篇文章主要為大家展示了“JavaScript中數(shù)組去重的示例分析”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“JavaScript中數(shù)組去重的示例分析”這篇文章吧。

一、前言:

我們?cè)趯?shí)際工作中,或者在面試找工作時(shí),都會(huì)用到或者被問到一個(gè)問題,那就是“數(shù)組如何去重”。是的,這個(gè)問題有很多種解決方案,看看下面的十種方式吧!

二、數(shù)組去重方式大匯總:

Methods 1: 思路:定義一個(gè)新數(shù)組,并存放原數(shù)組的第一個(gè)元素,然后將元素組一一和新數(shù)組的元素對(duì)比,若不同則存放在新數(shù)組中。

function unique(arr){
 var res = [arr[0]];
 for(var i=1; i<arr.length; i++){
  var repeat = false;
  for(var j=0; j<res.length; j++){
   if(arr[i] === res[j]){
    repeat = true;
    break;
   }
  }
  if(!repeat){
   res.push(arr[i]);
  }
 }
 return res;
}
console.log('------------方法一---------------');

console.log(unique([1,1,2,3,5,3,1,5,6,7,4]));

Methods 2: 思路:先將原數(shù)組排序,在與相鄰的進(jìn)行比較,如果不同則存入新數(shù)組。

function unique2(arr){
 var arr2 = arr.sort();
 var res = [arr2[0]];
 for(var i=1; i<arr2.length; i++){
  if(arr2[i] !== res[res.length-1]){
   res.push(arr2[i]);
  }
 } 
 return res;
}

console.log('------------方法二---------------');

console.log(unique2([1,1,2,3,5,3,1,5,6,7,4]));

Methods 3: 利用對(duì)象屬性存在的特性,如果沒有該屬性則存入新數(shù)組。

function unique3(arr){
 var res = [];
 var obj = {};
 for(var i=0; i<arr.length; i++){
  if( !obj[arr[i]] ){
   obj[arr[i]] = 1;
   res.push(arr[i]);
  }
 } 
 return res;
}

console.log('------------方法三---------------');

console.log(unique3([1,1,2,3,5,3,1,5,6,7,4]));

Methods 4: 利用數(shù)組的indexOf下標(biāo)屬性來查詢。

function unique4(arr){
 var res = [];
 for(var i=0; i<arr.length; i++){
  if(res.indexOf(arr[i]) == -1){
   res.push(arr[i]);
  }
 }
 return res;
}

console.log('------------方法四---------------');

console.log(unique4([1,1,2,3,5,3,1,5,6,7,4]));

Methods 5: 利用數(shù)組原型對(duì)象上的includes方法。

function unique5(arr){
 var res = [];
 
 for(var i=0; i<arr.length; i++){
  if( !res.includes(arr[i]) ){ // 如果res新數(shù)組包含當(dāng)前循環(huán)item
   res.push(arr[i]);
  }
 }
 return res;
}

console.log('------------方法五---------------');

console.log(unique5([1,1,2,3,5,3,1,5,6,7,4]));

Methods 6: 利用數(shù)組原型對(duì)象上的 filter 和 includes方法。

function unique6(arr){
 var res = [];
 
 res = arr.filter(function(item){
  return res.includes(item) ? '' : res.push(item);
 });
 return res;
}

console.log('------------方法六---------------');

console.log(unique6([1,1,2,3,5,3,1,5,6,7,4]));

Methods 7: 利用數(shù)組原型對(duì)象上的 forEach 和 includes方法。

function unique7(arr){
 var res = [];
 
 arr.forEach(function(item){
  res.includes(item) ? '' : res.push(item);
 }); 
 return res;
}

console.log('------------方法七---------------');

console.log(unique7([1,1,2,3,5,3,1,5,6,7,4]));

Methods 8: 利用數(shù)組原型對(duì)象上的 splice 方法。

function unique8(arr){
 var i,
  j,
  len = arr.length; 
 for(i = 0; i < len; i++){
  for(j = i + 1; j < len; j++){
   if(arr[i] == arr[j]){
    arr.splice(j,1);
    len--;
    j--;
   }
  }
 }
 return arr;
}

console.log('------------方法八---------------');

console.log(unique8([1,1,2,3,5,3,1,5,6,7,4]));

Methods 9: 利用數(shù)組原型對(duì)象上的 lastIndexOf 方法。

function unique9(arr){
 var res = []; 
 for(var i=0; i<arr.length; i++){
  res.lastIndexOf(arr[i]) !== -1 ? '' : res.push(arr[i]);
 }
 return res;
}

console.log('------------方法九---------------');

console.log(unique9([1,1,2,3,5,3,1,5,6,7,4]));

Methods 10: 利用 ES6的set 方法。

function unique10(arr){
 //Set數(shù)據(jù)結(jié)構(gòu),它類似于數(shù)組,其成員的值都是唯一的
 return Array.from(new Set(arr)); // 利用Array.from將Set結(jié)構(gòu)轉(zhuǎn)換成數(shù)組
}

console.log('------------方法十---------------');

console.log(unique10([1,1,2,3,5,3,1,5,6,7,4]));

 三、其他:

有意思的是,當(dāng)我把數(shù)組的長(zhǎng)度變得很大的時(shí)候(如下所示),測(cè)試了一下不同方法的執(zhí)行時(shí)間長(zhǎng)短,會(huì)發(fā)現(xiàn)方法三、四、五、六、七相對(duì)來說會(huì)更有優(yōu)勢(shì),而方法八的執(zhí)行速度似乎一直墊底。你也可以在你本地跑一下試試!

//測(cè)試用的超大數(shù)組
var arr = [1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2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,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6,7,4,1,1,2,3,5,3,1,5,6];
 
//這里只是舉例方法三(如果你把所有方法放在一起測(cè)試,會(huì)更直觀的看到執(zhí)行時(shí)間的差異)

var time3 = new Date().getTime();
function unique3(arr){
 var res = [];
 var obj = {};
 for(var i=0; i<arr.length; i++){
  if( !obj[arr[i]] ){
   obj[arr[i]] = 1;
   res.push(arr[i]);
  }
 } 
 return res;
}

console.log('------------方法三---------------');

console.log( unique3(arr) );

console.log('3所花時(shí)間: ' + (new Date().getTime() - time3));

以上是“JavaScript中數(shù)組去重的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI