JavaScript中的reduce()的5個(gè)用例
reduce()方法對(duì)數(shù)組中的每一個(gè)元素執(zhí)行一個(gè)reducer函數(shù)(由你提供),從而得到一個(gè)單一的輸出值。
reduce() 方法將一個(gè)數(shù)組中的所有元素還原成一個(gè)單一的輸出值,輸出值可以是數(shù)字、對(duì)象或字符串。reduce() 方法有兩個(gè)參數(shù),第一個(gè)是回調(diào)函數(shù),第二個(gè)是初始值。
回調(diào)函數(shù)
回調(diào)函數(shù)在數(shù)組的每個(gè)元素上執(zhí)行?;卣{(diào)函數(shù)的返回值是累加結(jié)果,并作為下一次調(diào)用回調(diào)函數(shù)的參數(shù)提供?;卣{(diào)函數(shù)帶有四個(gè)參數(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ù)組的第一個(gè)元素作為初始元素。
- arr.reduce(callback(accumulator, currentValue[,index[,array]])[, initialValue])
在下面的代碼片段中,第一個(gè)累加器(accumulator)被分配了初始值0。currentValue是正在處理的 numbersArr 數(shù)組的元素。在這里,currentValue 被添加到累加器,在下次調(diào)用回調(diào)函數(shù)時(shí),會(huì)將返回值作為參數(shù)提供。
- const numbersArr = [67, 90, 100, 37, 60];
- const total = numbersArr.reduce(function(accumulator, currentValue){
- console.log("accumulator is " + accumulator + " current value is " + currentValue);
- return accumulator + currentValue;
- }, 0);
- console.log("total : "+ total);
輸出
- accumulator is 0 current value is 67
- accumulator is 67 current value is 90
- accumulator is 157 current value is 100
- accumulator is 257 current value is 37
- accumulator is 294 current value is 60
- total : 354
JavaScript reduce用例
1.對(duì)數(shù)組的所有值求和
在下面的代碼中,studentResult 數(shù)組具有5個(gè)數(shù)字。使用 reduce() 方法,將數(shù)組減少為單個(gè)值,該值將 studentResult 數(shù)組的所有值和結(jié)果分配給 total。
- const studentResult = [67, 90, 100, 37, 60];
- const total = studentResult.reduce((accumulator, currentValue) => accumulator +currentValue, 0);
- console.log(total); // 354
2.對(duì)象數(shù)組中的數(shù)值之和
通常,我們從后端獲取數(shù)據(jù)作為對(duì)象數(shù)組,因此,reduce() 方法有助于管理我們的前端邏輯。在下面的代碼中,studentResult 對(duì)象數(shù)組有三個(gè)科目,這里,currentValue.marks 取了 studentResult 對(duì)象數(shù)組中每個(gè)科目的分?jǐn)?shù)。
- const studentResult = [
- { subject: '數(shù)學(xué)', marks: 78 },
- { subject: '物理', marks: 80 },
- { subject: '化學(xué)', marks: 93 }
- ];
- const total = studentResult.reduce((accumulator, currentValue) => accumulator + currentValue.marks, 0);
- console.log(total); // 251
3.展平數(shù)組
“展平數(shù)組”是指將多維數(shù)組轉(zhuǎn)換為一維。在下面的代碼中,twoDArr 2維數(shù)組被轉(zhuǎn)換為oneDArr 一維數(shù)組。此處,第一個(gè) [1,2] 數(shù)組分配給累加器 accumulator,然后 twoDArr 數(shù)組的其余每個(gè)元素都連接到累加器。
- const twoDArr = [ [1,2], [3,4], [5,6], [7,8] , [9,10] ];
- const oneDArr = twoDArr.reduce((accumulator, currentValue) => accumulator.concat(currentValue));
- console.log(oneDArr);
- // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
4.按屬性分組對(duì)象
根據(jù)對(duì)象的屬性,我們可以使用 reduce() 方法將對(duì)象數(shù)組分為幾組。通過下面的代碼片段,你可以清楚地理解這個(gè)概念。這里,result 對(duì)象數(shù)組有五個(gè)對(duì)象,每個(gè)對(duì)象都有 subject 和 marks 屬性。如果分?jǐn)?shù)大于或等于50,則該主題通過,否則,主題失敗。reduce() 用于將結(jié)果分組為通過和失敗。首先,將 initialValue 分配給累加器,然后 push() 方法在檢查條件之后將當(dāng)前對(duì)象添加到 pass 和 fail 屬性中作為對(duì)象數(shù)組。
- const result = [
- {subject: '物理', marks: 41},
- {subject: '化學(xué)', marks: 59},
- {subject: '高等數(shù)學(xué)', marks: 36},
- {subject: '應(yīng)用數(shù)學(xué)', marks: 90},
- {subject: '英語', marks: 64},
- ];
- let initialValue = {
- pass: [],
- fail: []
- }
- const groupedResult = result.reduce((accumulator, current) => {
- (current.marks >= 50) ? accumulator.pass.push(current) : accumulator.fail.push(current);
- return accumulator;
- }, initialValue);
- console.log(groupedResult);
輸出
- {
- pass: [
- { subject: ‘化學(xué)’, marks: 59 },
- { subject: ‘應(yīng)用數(shù)學(xué)’, marks: 90 },
- { subject: ‘英語’, marks: 64 }
- ],
- fail: [
- { subject: ‘物理’, marks: 41 },
- { subject: ‘高等數(shù)學(xué)’, marks: 36 }
- ]
- }
5.刪除數(shù)組中的重復(fù)項(xiàng)
在下面的代碼片段中,刪除了 plicatedArr 數(shù)組中的重復(fù)項(xiàng)。首先,將一個(gè)空數(shù)組分配給累加器作為初始值。accumulator.includes() 檢查 duplicatedArr 數(shù)組的每個(gè)元素是否已經(jīng)在累加器中可用。如果 currentValue 在累加器中不可用,則使用push() 將其添加。
- const duplicatedsArr = [1, 5, 6, 5, 7, 1, 6, 8, 9, 7];
- const removeDuplicatedArr = duplicatedsArr.reduce((accumulator, currentValue) => {
- if(!accumulator.includes(currentValue)){
- accumulator.push(currentValue);
- }
- return accumulator;
- }, []);
- console.log(removeDuplicatedArr);
- // [ 1, 5, 6, 7, 8, 9 ]
總結(jié)
在本文中,我們討論了數(shù)組 reduce() 方法。首先介紹 reduce() 方法,然后,使用一個(gè)簡(jiǎn)單的示例討論其行為。最后,通過示例討論了 reduce() 方法最常見的五個(gè)用例。如果你是JavaScript的初學(xué)者,那么本文將對(duì)你有所幫助。
本文轉(zhuǎn)載自微信公眾號(hào)「前端全棧開發(fā)者」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系前端全棧開發(fā)者公眾號(hào)。