自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

來(lái)一個(gè)老生常談的話題,JavaScript中,數(shù)組如何去重?

開發(fā) 前端
關(guān)于如何去除一個(gè)給定數(shù)組中的重復(fù)項(xiàng),應(yīng)該是 Javascript 面試中最常見的一個(gè)問(wèn)題了,最常見的方式有三種:Set、Array.prototype.filter 以及 Array.prototype.reduce,對(duì)于只有簡(jiǎn)單數(shù)據(jù)的數(shù)組來(lái)講,我最喜歡 Set,沒別的,就是寫起來(lái)簡(jiǎn)單。

[[274623]]

 關(guān)于如何去除一個(gè)給定數(shù)組中的重復(fù)項(xiàng),應(yīng)該是 Javascript 面試中最常見的一個(gè)問(wèn)題了,最常見的方式有三種:Set、Array.prototype.filter 以及 Array.prototype.reduce,對(duì)于只有簡(jiǎn)單數(shù)據(jù)的數(shù)組來(lái)講,我最喜歡 Set,沒別的,就是寫起來(lái)簡(jiǎ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

先讓我們來(lái)看看 Set 到底是個(gè)啥

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

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

  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 時(shí),也可以使用 Array.from(set)。

使用 Array.prototype.filter

要理解 filter 方法為什么可以去重,需要關(guān)注一下另一個(gè)方法 indexOf

  1. indexOf()方法返回在數(shù)組中可以找到一個(gè)給定元素的第一個(gè)索引,如果不存在,則返回 -1。 
  2. <cite>https://developer.mozilla.org...</cite> 
  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)建一個(gè)新數(shù)組, 其包含通過(guò)所提供函數(shù)實(shí)現(xiàn)的測(cè)試的所有元素。 
  2. <cite>https://developer.mozilla.org...</cite> 

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

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

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

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

使用 Array.prototype.reduce

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

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

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

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

  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} 個(gè)值`) 
  6.   console.log('當(dāng)前累計(jì)器:'unique
  7.   console.log('當(dāng)前值:', item) 
  8.   console.log('是否已添加進(jìn)累計(jì)器?', exist) 
  9.   console.log('新值'next
  10.   console.groupEnd() 
  11.   return next 
  12. }, []) 

 

責(zé)任編輯:華軒 來(lái)源: segmentfault
相關(guān)推薦

2015-07-21 13:39:58

Javascript作用域

2015-06-25 10:46:23

數(shù)據(jù)中心節(jié)能

2015-04-08 11:50:07

數(shù)據(jù)加密數(shù)據(jù)泄露

2014-07-25 13:34:08

2010-07-28 09:09:55

SQL

2022-03-08 15:01:48

負(fù)載均衡IP服務(wù)器

2012-02-13 09:46:56

數(shù)據(jù)中心耗能服務(wù)器虛擬化

2014-08-21 10:25:44

網(wǎng)絡(luò)安全兒童賬戶Google

2011-08-18 14:47:06

2020-01-14 10:37:38

存儲(chǔ)DateTime數(shù)值

2021-04-12 09:36:25

Redis擊穿穿透

2021-04-12 09:58:46

Redis數(shù)據(jù)庫(kù)代碼

2017-04-11 13:54:49

HTTPURLHTML

2010-03-29 09:04:51

VB.NET

2022-07-28 00:25:22

5G4G速度

2022-05-06 12:01:01

優(yōu)化小程序

2021-11-26 05:57:12

開源備份Restic

2024-01-02 09:42:17

C#開發(fā)Windows消息循環(huán)機(jī)制

2010-01-28 13:55:45

三層交換機(jī)

2020-11-18 07:52:57

技巧
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)