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

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
JavaScript中的reduce()的5個用例

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

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(當前值)——處理數(shù)組的當前元素。
  • Current Index(當前索引)——處理數(shù)組當前元素的索引。
  • 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ù)組中每個科目的分數(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 屬性。如果分數(shù)大于或等于50,則該主題通過,否則,主題失敗。reduce() 用于將結(jié)果分組為通過和失敗。首先,將 initialValue 分配給累加器,然后 push() 方法在檢查條件之后將當前對象添加到 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é)者,那么本文將對你有所幫助。


本文題目:JavaScript中的reduce()的5個用例
當前鏈接:http://www.5511xx.com/article/djshecs.html