日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
來一個老生常談的話題,JavaScript中,數(shù)組如何去重?

 關于如何去除一個給定數(shù)組中的重復項,應該是 Javascript 面試中最常見的一個問題了,最常見的方式有三種:Set、Array.prototype.filter 以及 Array.prototype.reduce,對于只有簡單數(shù)據(jù)的數(shù)組來講,我最喜歡 Set,沒別的,就是寫起來簡單。

專注于為中小企業(yè)提供成都網(wǎng)站建設、成都做網(wǎng)站服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)玉林免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上1000+企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。

 
 
 
 
  1. const originalArray = [1, 2, '咩', 1, 'Super Ball', '咩', '咩', 'Super Ball', 4] 
  2. const bySet = [...new Set(originalArray)] 
  3. const byFilter = originalArray.filter((item, index) => originalArray.indexOf(item) === index)  
  4. const byReduce = originalArray.reduce((unique, item) => unique.includes(item) ? unique : [...unique, item], []) 

使用 Set

先讓我們來看看 Set 到底是個啥

 
 
 
 
  1. Set 對象允許你存儲任何類型的唯一值,無論是原始值或者是對象引用。 
  2. https://developer.mozilla.org... 
  • 首先,Set 中只允許出現(xiàn)唯一值
  • 唯一性是比對原始值或者對象引用

const bySet = [...new Set(originalArray)] 這一段的操作,我們將它拆分來看:

 
 
 
 
  1. const originalArray = [1, 2, '咩', 1, 'Super Ball', '咩', '咩', 'Super Ball', 4]  
  2. const uniqueSet = new Set(originalArray) 
  3. // 得到 Set(5) [ 1, 2, "咩", "Super Ball", 4 ] 
  4. const bySet = [...uniqueSet] 
  5. // 得到 Array(5) [ 1, 2, "咩", "Super Ball", 4 ] 

在將 Set 轉(zhuǎn)為 Array 時,也可以使用 Array.from(set)。

使用 Array.prototype.filter

要理解 filter 方法為什么可以去重,需要關注一下另一個方法 indexOf

 
 
 
 
  1. indexOf()方法返回在數(shù)組中可以找到一個給定元素的第一個索引,如果不存在,則返回 -1。 
  2. https://developer.mozilla.org... 
 
 
 
 
  1. const beasts = ['ant', 'bison', 'camel', 'duck', 'bison']; 
  2. console.log(beasts.indexOf('bison')); 
  3. // expected output: 1 
  4. // start from index 2 
  5. console.log(beasts.indexOf('bison', 2)); 
  6. // expected output: 4 
  7. console.log(beasts.indexOf('giraffe')); 
  8. // expected output: -1 
 
 
 
 
  1. filter() 方法創(chuàng)建一個新數(shù)組, 其包含通過所提供函數(shù)實現(xiàn)的測試的所有元素。 
  2. https://developer.mozilla.org... 

filter 方法接受兩個參數(shù):

  • 第一個參數(shù):一個回調(diào)函數(shù), filter 會將數(shù)據(jù)中的每一項都傳遞給該函數(shù),若該函數(shù)返回 真值,則數(shù)據(jù)保存,返回 假值,則數(shù)據(jù)將不會出現(xiàn)在新生成的數(shù)據(jù)中
  • 第二個參數(shù):回調(diào)函數(shù)中 this 的指向

我們將上面的去重方法按下面這樣重寫一下,就可以看清整個 filter 的執(zhí)行過程了。

 
 
 
 
  1. const originalArray = [1, 2, '咩', 1, 'Super Ball', '咩', '咩', 'Super Ball', 4] 
  2. const table = [] 
  3. const byFilter = originalArray.filter((item, index) => { 
  4.   // 如果找到的索引與當前索引一致,則保留該值 
  5.   const shouldKeep = originalArray.indexOf(item) === index 
  6.   table.push({ 
  7.     序號: index, 
  8.     值: item, 
  9.     是否應該保留: shouldKeep ? '保留' : '刪除' 
  10.   }) 
  11.   return shouldKeep 
  12. })  
  13. console.log(byFilter) 
  14. console.table(table) 

使用 Array.prototype.reduce

 
 
 
 
  1. reduce() 方法對數(shù)組中的每個元素執(zhí)行一個由您提供的 reducer 函數(shù)(升序執(zhí)行),將其結果匯總為單個返回值。 
  2. https://developer.mozilla.org... 

Array.prototype.reduce 方法接受兩個參數(shù):

  • Callback:回調(diào)函數(shù),它可以接收四個參數(shù)
  1. Accumulator:累計器,這個其實是讓很多人忽略的一點,就是,累計器其實可以是任何類型的數(shù)據(jù)
  2. Current Value:當前值
  3. Current Index:當前值的索引
  4. Source Array:源數(shù)組
  • Initial Value:累計器的初始值,就跟累計器一樣,這個參數(shù)也總是被絕大多數(shù)人忽略

就像 filter 章節(jié)一樣,我們來看看 reduce 的執(zhí)行過程:

 
 
 
 
  1. const originalArray = [1, 2, '咩', 1, 'Super Ball', '咩', '咩', 'Super Ball', 4] 
  2. const byReduce = originalArray.reduce((unique, item, index, source) => { 
  3.   const exist = unique.includes(item) 
  4.   const next = unique.includes(item) ? unique : [...unique, item] 
  5.   console.group(`遍歷第 ${index} 個值`) 
  6.   console.log('當前累計器:', unique) 
  7.   console.log('當前值:', item) 
  8.   console.log('是否已添加進累計器?', exist) 
  9.   console.log('新值', next) 
  10.   console.groupEnd() 
  11.   return next 
  12. }, []) 

網(wǎng)站題目:來一個老生常談的話題,JavaScript中,數(shù)組如何去重?
分享地址:http://www.5511xx.com/article/cojdogh.html