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

使用 JavaScript 進(jìn)行數(shù)據(jù)分組最優(yōu)雅的方式

開發(fā) 前端
對數(shù)據(jù)進(jìn)行分組,是我們在開發(fā)中經(jīng)常會遇到的需求,使用 JavaScript 進(jìn)行數(shù)據(jù)分組的方式也有很多種,但是由于沒有原生方法的支持,我們自己實現(xiàn)的數(shù)據(jù)分組函數(shù)通常都比較冗長而且難以理解。

大家好,我是 ConardLi ,今天我們一起來看一個數(shù)據(jù)分組的小技巧。

對數(shù)據(jù)進(jìn)行分組,是我們在開發(fā)中經(jīng)常會遇到的需求,使用 JavaScript 進(jìn)行數(shù)據(jù)分組的方式也有很多種,但是由于沒有原生方法的支持,我們自己實現(xiàn)的數(shù)據(jù)分組函數(shù)通常都比較冗長而且難以理解。

不過,告訴大家一個好消息,一個專門用來做數(shù)據(jù)分組的提案 Array.prototype.groupBy 已經(jīng)到達(dá) Stage 3 啦!

在看這個提案,之前,我們先來回顧下我們以前在 JavaScript 里是怎么分組的。

以前的方式

假設(shè)我們有下面一組數(shù)據(jù):

  1. const items = [ 
  2.   { 
  3.     type: 'clothes', 
  4.     value: '👔', 
  5.   }, 
  6.   { 
  7.     type: 'clothes', 
  8.     value: '👕', 
  9.   }, 
  10.   { 
  11.     type: 'clothes', 
  12.     value: '👗', 
  13.   }, 
  14.   { 
  15.     type: 'animal', 
  16.     value: '🐷', 
  17.   }, 
  18.   { 
  19.     type: 'animal', 
  20.     value: '🐸', 
  21.   }, 
  22.   { 
  23.     type: 'animal', 
  24.     value: '🐒', 
  25.   }, 
  26. ]; 

我們希望按照 type 分組成下面的格式:

  1. const items = { 
  2.   clothes: [ 
  3.     { 
  4.       type: 'clothes', 
  5.       value: '👔', 
  6.     }, 
  7.     { 
  8.       type: 'clothes', 
  9.       value: '👕', 
  10.     }, 
  11.     { 
  12.       type: 'clothes', 
  13.       value: '👗', 
  14.     }, 
  15.   ], 
  16.   animal: [ 
  17.     { 
  18.       type: 'animal', 
  19.       value: '🐷', 
  20.     }, 
  21.     { 
  22.       type: 'animal', 
  23.       value: '🐸', 
  24.     }, 
  25.     { 
  26.       type: 'animal', 
  27.       value: '🐒', 
  28.     }, 
  29.   ], 
  30. }; 

我們可能會用到下面的寫法:

for 循環(huán)

最直接而且容易理解的方法,就是代碼有點多。

  1. const groupedBy = {}; 
  2.  
  3. for (const item of items) { 
  4.   if (groupedBy[item.type]) { 
  5.     groupedBy[item.type].push(item); 
  6.   } else { 
  7.     groupedBy[item.type] = [item]; 
  8.   } 

reduce

使用 Array.protoype.reduce 雖然語法看起來簡單,但是太難讀了。

  1. items.reduce( 
  2.   (acc, item) => ({ 
  3.     ...acc, 
  4.     [item.type]: [...(acc[item.type] ?? []), item], 
  5.   }), 
  6.   {}, 
  7. ); 

我們稍微改造的容易理解一點,語法就跟上面的 for 循環(huán)差不多了:

  1. items.reduce((acc, item) => { 
  2.   if (acc[item.type]) { 
  3.     acc[item.type].push(item); 
  4.   } else { 
  5.     acc[item.type] = [item]; 
  6.   } 
  7.  
  8.   return acc; 
  9. }, {}); 

filter

使用 Array.prototype.filter,代碼看起來很容易閱讀,但是性能很差,你需要對數(shù)組進(jìn)行多次過濾,而且如果 type 屬性值比較多的情況下,還需要做更多的 filter 操作。

  1. const groupedBy = { 
  2.   fruit: items.filter((item) => item.type === 'clothes'), 
  3.   vegetable: items.filter((item) => item.type === 'animal'), 
  4. }; 

其他

如果你既不想用 reduce,還想用到函數(shù)式寫法,你可能會寫出下面的代碼:

  1. Object.fromEntries( 
  2.   Array.from(new Set(items.map(({ type }) => type))).map((type) => [ 
  3.     type, 
  4.     items.filter((item) => item.type === type), 
  5.   ]), 
  6. ); 

是不是很讓人崩潰 🤯~

Array.prototype.groupBy

好了,如果使用 Array.prototype.groupBy,你只需要下面這一行代碼:

  1. items.groupBy(({ type }) => type); 

groupBy 的回調(diào)中一共有三個參數(shù):

  • 參數(shù)1:數(shù)組遍歷到的當(dāng)前對象
  • 參數(shù)2:index 索引
  • 參數(shù)3:原數(shù)組
  1. const array = [1, 2, 3, 4, 5]; 
  2.  
  3. // groupBy groups items by arbitrary key. 
  4. // In this case, we're grouping by even/odd keys 
  5. array.groupBy((num, index, array) => { 
  6.   return num % 2 === 0 ? 'even': 'odd'; 
  7. }); 

另外,你還可以用 groupByToMap,將數(shù)據(jù)分組為一個 Map 對象。

  1. // groupByToMap returns items in a Map, and is useful for grouping using 
  2. // an object key. 
  3. const odd  = { odd: true }; 
  4. const even = { even: true }; 
  5. array.groupByToMap((num, index, array) => { 
  6.   return num % 2 === 0 ? even: odd; 
  7. }); 
  8.  
  9. // =>  Map { {odd: true}: [1, 3, 5], {even: true}: [2, 4] } 

 

責(zé)任編輯:趙寧寧 來源: code秘密花園
相關(guān)推薦

2017-03-20 16:30:15

Android退出應(yīng)用優(yōu)雅方式

2025-01-26 00:00:35

2017-10-31 11:55:46

sklearn數(shù)據(jù)挖掘自動化

2017-02-16 08:41:09

數(shù)據(jù)Vlookup匹配

2022-11-02 14:45:24

Python數(shù)據(jù)分析工具

2009-03-16 10:29:45

數(shù)據(jù)挖掘過濾器Access

2009-09-08 16:50:12

使用LINQ進(jìn)行數(shù)據(jù)轉(zhuǎn)

2019-09-30 10:12:21

機(jī)器學(xué)習(xí)數(shù)據(jù)映射

2010-03-29 18:31:09

Nginx配置

2022-03-28 14:08:02

Python數(shù)據(jù)清洗數(shù)據(jù)集

2023-08-15 16:20:42

Pandas數(shù)據(jù)分析

2022-04-08 11:25:58

數(shù)據(jù)庫操作AbilityData

2009-07-16 14:46:48

jdbc statem

2021-07-27 15:40:39

Python數(shù)據(jù)清洗函數(shù)

2022-01-26 09:00:00

數(shù)據(jù)庫SnowparkSQL

2023-10-18 18:38:44

數(shù)據(jù)校驗業(yè)務(wù)

2011-10-14 14:24:26

Ruby

2022-06-02 13:59:57

數(shù)據(jù)遷移數(shù)據(jù)

2021-07-17 22:41:53

Python數(shù)據(jù)技術(shù)

2024-10-28 12:57:36

Pandas數(shù)據(jù)清洗
點贊
收藏

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