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

厲害了,JavaScript 新提案:Aray.groupBy()

開發(fā) 后端
許多開發(fā)人員喜歡 Ruby 編程語言,因為它具有豐富的標(biāo)準(zhǔn)實用程序庫。例如,Ruby中的數(shù)組有大量的方法。

許多開發(fā)人員喜歡 Ruby 編程語言,因為它具有豐富的標(biāo)準(zhǔn)實用程序庫。例如,Ruby中的數(shù)組有大量的方法。

不過,我們的JavaScript也在努力,在字符串和數(shù)組方面逐步豐富了它的標(biāo)準(zhǔn)庫。例如,在以前的文章中,介紹新的 array.at() 方法。

今天我們在來看新的數(shù)組組提案(目前處于第三階段),它引入了新方法 array.groupby() 和array.groupbytomap() 。它們的 polyfills 文件可以在core-js 庫中找到。

接著,我們來看下能從中學(xué)到些什么。

1. array.groupBy()

假設(shè)我們有一個產(chǎn)品列表,其中每個產(chǎn)品都是一個具有2個屬性的對象: name 和 category。

  1. const products = [ 
  2.   { name'apples', category: 'fruits' }, 
  3.   { name'oranges', category: 'fruits' }, 
  4.   { name'potatoes', category: 'vegetables' } 
  5. ]; 

在上面的示例中,products 是一個產(chǎn)品對象數(shù)組。

現(xiàn)在,對產(chǎn)品列表執(zhí)行一個簡單的操作,將產(chǎn)品按類別分組。

  1. const groupByCategory = { 
  2.   'fruits': [ 
  3.     { name'apples', category: 'fruits' },  
  4.     { name'oranges', category: 'fruits' }, 
  5.   ], 
  6.   'vegetables': [ 
  7.     { name'potatoes', category: 'vegetables' } 
  8.   ] 
  9. }; 

如何從 products 數(shù)組中得到一個類似 groupByCategory 的數(shù)組?

通常的方法是使用array.reduce()來實現(xiàn),如下所示:

  1. const groupByCategory = products.reduce((group, product) => { 
  2.   const { category } = product; 
  3.   group[category] = group[category] ?? []; 
  4.   group[category].push(product); 
  5.   return group
  6. }, {}); 
  7. console.log(groupByCategory); 
  8. // { 
  9. //   'fruits': [ 
  10. //     { name'apples', category: 'fruits' },  
  11. //     { name'oranges', category: 'fruits' }, 
  12. //   ], 
  13. //   'vegetables': [ 
  14. //     { name'potatoes', category: 'vegetables' } 
  15. //   ] 
  16. // } 

products.reduce((acc, product) => { ... }) 將產(chǎn)品數(shù)組還原為一個按類別分組的產(chǎn)品對象。

array.reduce()方法有用且強(qiáng)大,但有時它的可讀性并不是最好的。

因為分組數(shù)據(jù)是常見的事(從SQL中召回groupby ?),數(shù)組組提案引入了兩個有用的方法:array. groupBy()和 array.groupByToMap()。

下面介紹如何使用 array.groupBy() 創(chuàng)建相同的分類分組:

  1. const groupByCategory = products.groupBy(product => { 
  2.   return product.category; 
  3. }); 
  4.  
  5. console.log(groupByCategory);  
  6.  
  7. // { 
  8. //   'fruits': [ 
  9. //     { name'apples', category: 'fruits' },  
  10. //     { name'oranges', category: 'fruits' }, 
  11. //   ], 
  12. //   'vegetables': [ 
  13. //     { name'potatoes', category: 'vegetables' } 
  14. //   ] 
  15. // } 

products.groupBy(product => {...}) 返回一個對象,其中每個屬性的鍵是類別名稱,值是對應(yīng)類別的產(chǎn)品數(shù)組。

使用 products.groupBy() 分組比使用 product.reduce() 代碼更少,更容易理解。

array.groupBy(callback) 接受一個回調(diào)函數(shù),該函數(shù)被調(diào)用時有3個參數(shù):當(dāng)前數(shù)組項、索引和數(shù)組本身?;卣{(diào)函數(shù)應(yīng)該返回一個字符串:你想添加項目的組名。

  1. const groupedObject = array.groupBy((item, index, array) => { 
  2.   // ... 
  3.   return groupNameAsString; 
  4. }); 

2. array.groupByToMap()

有時你可能想使用 Map 而不是普通對象。 Map 的好處是它可以接受任何數(shù)據(jù)類型作為鍵,但普通對象只限于字符串和 symbol。

恩,如果你想把數(shù)據(jù)分組到一個Map中,你可以使用 array.groupByToMap() 方法。

array.groupByToMap(callback)的工作方式與 array.groupBy(callback) 完全一樣,只是它將項目分組到 Map 中,而不是一個普通的 JS 對象中。

例如,將產(chǎn)品數(shù)組按類別名稱分組到一個 ap 中,執(zhí)行方法如下。

  1. const groupByCategory = products.groupByToMap(product => { 
  2.   return product.category; 
  3. }); 
  4.  
  5. console.log(groupByCategory);  
  6.  
  7. // Map([ 
  8. //   ['fruits', [ 
  9. //     { name'apples', category: 'fruits' },  
  10. //     { name'oranges', category: 'fruits' }, 
  11. //   ]], 
  12. //   ['vegetables', [ 
  13. //     { name'potatoes', category: 'vegetables' } 
  14. //   ] 
  15. // ]) 

3.總結(jié)

如果你想輕松地對數(shù)組中的項進(jìn)行分組(類似于SQL中的GROUP BY),那么歡迎使用新方法array.groupBy() 和 array.groupByToMap()。

兩個函數(shù)都接受一個回調(diào)函數(shù),該回調(diào)函數(shù)應(yīng)返回必須插入當(dāng)前項的組的鍵。

array.groupBy()將這些項分組為一個普通的JavaScript對象,而array.groupByToMap()將它們分組為一個 Map 實例。

如果你想馬上使用這些函數(shù),那么使用 core-js 庫提供的 polyfill。

作者:Ashish Lahoti 譯者:前端小智 來源:dmitripavlutin 原文:https://dmitripavlutin.com/javascript-array-group/

本文轉(zhuǎn)載自微信公眾號「大遷世界」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系大遷世界公眾號。

 

責(zé)任編輯:武曉燕 來源: 大遷世界
相關(guān)推薦

2021-12-27 07:59:50

ECMAScript JSON模塊Node.js

2021-09-17 12:18:53

NginxJavaScript前端

2017-02-23 08:00:04

智能語音Click

2018-04-11 14:30:33

2018-05-14 22:58:14

戴爾

2021-03-01 12:06:12

Nginx命令Linux

2023-05-06 06:47:46

Bing聊天機(jī)器人

2021-11-01 07:50:44

TomcatWeb應(yīng)用

2020-06-08 17:35:27

Redis集群互聯(lián)網(wǎng)

2022-04-08 08:11:28

Python代碼

2020-03-10 13:35:23

Gihub搜索開源

2021-06-03 09:30:30

Python操作注冊表regedit

2022-01-25 08:36:29

array.flat映射函數(shù)數(shù)組

2019-11-25 21:53:48

代碼算法BUG

2022-05-03 23:44:21

Python動態(tài)鏈接庫Ctypes

2017-07-27 16:51:19

數(shù)字化環(huán)衛(wèi)信息化

2021-05-15 08:02:33

HashMap 散列函數(shù)哈希沖突

2020-06-09 07:42:30

重命名文件 Linux

2018-01-24 10:48:34

神經(jīng)網(wǎng)絡(luò)深度學(xué)習(xí)前端

2017-02-20 10:17:53

華為
點贊
收藏

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