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

JavaScript中的reduce()的5個用例

開發(fā) 前端
reduce()方法對數(shù)組中的每一個元素執(zhí)行一個reducer函數(shù)(由你提供),從而得到一個單一的輸出值。

 reduce()方法對數(shù)組中的每一個元素執(zhí)行一個reducer函數(shù)(由你提供),從而得到一個單一的輸出值。

[[334138]]

reduce() 方法將一個數(shù)組中的所有元素還原成一個單一的輸出值,輸出值可以是數(shù)字、對象或字符串。reduce() 方法有兩個參數(shù),第一個是回調(diào)函數(shù),第二個是初始值。

回調(diào)函數(shù)

回調(diào)函數(shù)在數(shù)組的每個元素上執(zhí)行。回調(diào)函數(shù)的返回值是累加結(jié)果,并作為下一次調(diào)用回調(diào)函數(shù)的參數(shù)提供?;卣{(diào)函數(shù)帶有四個參數(shù)。

  • Accumulator(累加器)——累加器累加回調(diào)函數(shù)的返回值。
  • Current Value(當(dāng)前值)——處理數(shù)組的當(dāng)前元素。
  • Current Index(當(dāng)前索引)——處理數(shù)組當(dāng)前元素的索引。
  • Source Array(源數(shù)組)

Current Index 和 Source Array 是可選的。

初始值

如果指定了初始值,則將累加器設(shè)置為 initialValue 作為初始元素。否則,將累加器設(shè)置為數(shù)組的第一個元素作為初始元素。

 

  1. arr.reduce(callback(accumulator, currentValue[,index[,array]])[, initialValue]) 

在下面的代碼片段中,第一個累加器(accumulator)被分配了初始值0。currentValue 是正在處理的 numbersArr 數(shù)組的元素。在這里,currentValue 被添加到累加器,在下次調(diào)用回調(diào)函數(shù)時,會將返回值作為參數(shù)提供。

 

  1. const numbersArr = [67, 90, 100, 37, 60]; 
  2.  
  3. const total = numbersArr.reduce(function(accumulator, currentValue){ 
  4.     console.log("accumulator is " + accumulator + " current value is " + currentValue); 
  5.     return accumulator + currentValue; 
  6. }, 0); 
  7.  
  8. console.log("total : "+ total); 

輸出

 

  1. accumulator is 0 current value is 67 
  2. accumulator is 67 current value is 90 
  3. accumulator is 157 current value is 100 
  4. accumulator is 257 current value is 37 
  5. accumulator is 294 current value is 60 
  6. total : 354 

JavaScript reduce用例

1.對數(shù)組的所有值求和

在下面的代碼中,studentResult 數(shù)組具有5個數(shù)字。使用 reduce() 方法,將數(shù)組減少為單個值,該值將 studentResult 數(shù)組的所有值和結(jié)果分配給 total。

 

  1. const studentResult = [67, 90, 100, 37, 60]; 
  2.  
  3. const total = studentResult.reduce((accumulator, currentValue) => accumulator +currentValue, 0); 
  4.  
  5. console.log(total); // 354 

2.對象數(shù)組中的數(shù)值之和

通常,我們從后端獲取數(shù)據(jù)作為對象數(shù)組,因此,reduce() 方法有助于管理我們的前端邏輯。在下面的代碼中,studentResult 對象數(shù)組有三個科目,這里,currentValue.marks 取了 studentResult 對象數(shù)組中每個科目的分?jǐn)?shù)。

 

  1. const studentResult = [ 
  2.   { subject: '數(shù)學(xué)', marks: 78 }, 
  3.   { subject: '物理', marks: 80 }, 
  4.   { subject: '化學(xué)', marks: 93 } 
  5. ]; 
  6.  
  7. const total = studentResult.reduce((accumulator, currentValue) => accumulator + currentValue.marks, 0); 
  8.  
  9. console.log(total); // 251 

3.展平數(shù)組

“展平數(shù)組”是指將多維數(shù)組轉(zhuǎn)換為一維。在下面的代碼中,twoDArr 2維數(shù)組被轉(zhuǎn)換為 oneDArr 一維數(shù)組。此處,第一個 [1,2] 數(shù)組分配給累加器 accumulator,然后 twoDArr 數(shù)組的其余每個元素都連接到累加器。

 

  1. const twoDArr = [ [1,2], [3,4], [5,6], [7,8] , [9,10] ]; 
  2.  
  3. const oneDArr = twoDArr.reduce((accumulator, currentValue) => accumulator.concat(currentValue)); 
  4.  
  5. console.log(oneDArr); 
  6. // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] 

4.按屬性分組對象

根據(jù)對象的屬性,我們可以使用 reduce() 方法將對象數(shù)組分為幾組。通過下面的代碼片段,你可以清楚地理解這個概念。這里,result 對象數(shù)組有五個對象,每個對象都有 subject 和 marks 屬性。如果分?jǐn)?shù)大于或等于50,則該主題通過,否則,主題失敗。reduce() 用于將結(jié)果分組為通過和失敗。首先,將 initialValue 分配給累加器,然后 push() 方法在檢查條件之后將當(dāng)前對象添加到 pass 和 fail 屬性中作為對象數(shù)組。

 

  1. const result = [ 
  2.   {subject: '物理', marks: 41}, 
  3.   {subject: '化學(xué)', marks: 59}, 
  4.   {subject: '高等數(shù)學(xué)', marks: 36}, 
  5.   {subject: '應(yīng)用數(shù)學(xué)', marks: 90}, 
  6.   {subject: '英語', marks: 64}, 
  7. ]; 
  8.  
  9. let initialValue = { 
  10.   pass: [], 
  11.   fail: [] 
  12.  
  13. const groupedResult = result.reduce((accumulator, current) => { 
  14.   (current.marks >= 50) ? accumulator.pass.push(current) : accumulator.fail.push(current); 
  15.   return accumulator; 
  16. }, initialValue); 
  17.  
  18. console.log(groupedResult); 

輸出

 

  1.  pass: [ 
  2.   { subject: ‘化學(xué)’, marks: 59 }, 
  3.   { subject: ‘應(yīng)用數(shù)學(xué)’, marks: 90 }, 
  4.   { subject: ‘英語’, marks: 64 } 
  5.  ], 
  6.  fail: [ 
  7.   { subject: ‘物理’, marks: 41 }, 
  8.   { subject: ‘高等數(shù)學(xué)’, marks: 36 } 
  9.  ] 

5.刪除數(shù)組中的重復(fù)項

在下面的代碼片段中,刪除了 plicatedArr 數(shù)組中的重復(fù)項。首先,將一個空數(shù)組分配給累加器作為初始值。accumulator.includes() 檢查 duplicatedArr 數(shù)組的每個元素是否已經(jīng)在累加器中可用。如果 currentValue 在累加器中不可用,則使用 push() 將其添加。

 

  1. const duplicatedsArr = [1, 5, 6, 5, 7, 1, 6, 8, 9, 7]; 
  2.  
  3. const removeDuplicatedArr = duplicatedsArr.reduce((accumulator, currentValue) => { 
  4.   if(!accumulator.includes(currentValue)){ 
  5.     accumulator.push(currentValue); 
  6.   } 
  7.   return accumulator; 
  8. }, []); 
  9.  
  10. console.log(removeDuplicatedArr); 
  11. // [ 1, 5, 6, 7, 8, 9 ] 

總結(jié)在本文中,我們討論了數(shù)組 reduce() 方法。首先介紹 reduce() 方法,然后,使用一個簡單的示例討論其行為。最后,通過示例討論了 reduce() 方法最常見的五個用例。如果你是JavaScript的初學(xué)者,那么本文將對你有所幫助。

 

責(zé)任編輯:華軒 來源: 前端全棧開發(fā)者
相關(guān)推薦

2020-07-29 07:52:41

JavaScript

2019-12-05 10:59:08

云計算存儲數(shù)據(jù)

2020-08-01 15:37:19

5G無線技術(shù)網(wǎng)絡(luò)

2021-06-03 19:28:44

LPWAN環(huán)境監(jiān)測工業(yè)4.0

2020-07-22 01:30:11

農(nóng)業(yè)物聯(lián)網(wǎng)IOT

2022-04-04 21:26:05

物聯(lián)網(wǎng)汽車

2019-12-03 19:09:19

JavaScriptNumbers阿里云計算

2022-07-06 10:04:45

JavaScript數(shù)組前端

2020-06-05 11:15:53

物聯(lián)網(wǎng)醫(yī)療保健技術(shù)

2022-06-08 10:42:34

ReduceJavaScript技巧

2021-09-28 09:19:02

CIO5G首席信息官

2022-11-15 10:01:27

2018-08-21 05:03:04

NV overlay網(wǎng)絡(luò)虛擬化網(wǎng)絡(luò)

2024-04-17 16:26:25

邊緣計算云計算制造業(yè)

2021-07-01 11:56:51

JavaScript開發(fā)代碼

2021-02-07 09:49:45

人工智能AI物聯(lián)網(wǎng)

2010-07-08 11:27:00

UML用例建模

2024-09-24 15:36:09

2020-02-06 15:17:13

物聯(lián)網(wǎng)醫(yī)療保健邊緣計算

2010-06-09 18:38:28

UML用例圖
點贊
收藏

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