JavaScript reduce方法使用方法介紹
1. reduce方法的使用
Array.prototype.reduce(callBack(previousValue, currentValue, currentIndex), initialValue)
reduce是數(shù)組的一個(gè)高階方法,用來(lái)實(shí)現(xiàn)對(duì)數(shù)組的累加計(jì)算
var arr = ['a', 'b', 'c'];
var str = arr.reduce(function (previousValue, currentValue, currentIndex) {
return previousValue + currentValue;
});
console.log(str); //abc
var str = arr.reduce(function (previousValue, currentValue, currentIndex) {
return previousValue + currentValue;
}, 's');
console.log(str); //sabcreduce的回調(diào)函數(shù)中一共有4個(gè)參數(shù):
- previous 表示計(jì)算的前一個(gè)值
- currentValue 是正在被計(jì)算的值
- currentIndex 是正在計(jì)算的索引
- initialValue 設(shè)置累加的初始值
在數(shù)組遍歷的時(shí)候,回調(diào)函數(shù)的返回值會(huì)累加給previousValue,一直到數(shù)組遍歷完畢返回這個(gè)累加值。
在沒(méi)有傳遞initialValue的情況下,首次累加的時(shí)候previousValue為數(shù)組的第1項(xiàng),currentValue為數(shù)組的第2項(xiàng),當(dāng)傳遞了initialValue的時(shí)候previousValue初始值為initialValue,currentValue初始值為數(shù)組的第一項(xiàng)
2. reduce數(shù)組的使用場(chǎng)景
2.1 扁平化數(shù)組
使用reduce實(shí)現(xiàn)Array.prototype.flat的功能
const arr = [[1, 2, 3], [4, 5, 6, 7, 8], [9]];
const flat = arr.reduce((previousValue, currentValue) => {
return previousValue.concat(currentValue);
});
console.log(flat); //[1, 2, 3, 4, 5, 6, 7, 8, 9]2.2 數(shù)組去重
const arr = [1, 2, 5, 2, 5, 5];
const deduplication = arr.reduce((previousValue, currentValue) => {
return previousValue.includes(currentValue) ? previousValue : previousValue.concat([currentValue]);
}, []);
console.log(deduplication);2.3 計(jì)算數(shù)組最大/最小值
const arr = [2, 5, 1, 88, 12, -21, 33, 10, 75]; const maxVal = arr.reduce((previousValue, currentValue) => Math.max(previousValue, currentValue)); console.log(maxVal); //8
2.4 數(shù)組求和
const arr = [1, 2, 3, 4, 5]; const sum = arr.reduce((previousValue, currentValue) => previousValue + currentValue); console.log(sum); //15
2.5 計(jì)算數(shù)組中元素的出現(xiàn)次數(shù)
const arr = ['a', 'c', 'b', 'a', 'c', 'e'];
const countedCharacter = arr.reduce((previousValue, currentValue) => {
if (currentValue in previousValue) {
return { ...previousValue, [currentValue]: previousValue[currentValue] + 1 };
} else {
return { ...previousValue, [currentValue]: 1 };
};
}, {});
console.log(countedCharacter); //{a: 2, c: 2, b: 1, e: 1};3. 操作對(duì)象
累加對(duì)象數(shù)組里面的值
const arr = [{ x: 1 }, { x: 2 }, { x: 3 }];
const sum = arr.reduce((previousValue, currentValue) =>
previousValue + currentValue.x, 0);
console.log(sum); //6
4. 使用reduce代替.filter().map()
使用reduce可以同時(shí)完成過(guò)濾合映射,不需要遍歷兩次數(shù)組
const arr = [81, 92, 67, 100, 79];
const scoreArr = arr.filter(s => s > 80).map(val => ({ score: val }));
const scoreArr1 = arr.reduce((previousValue, currentValue) => {
if (currentValue > 80) { return previousValue.concat([{ score: currentValue }]) } else {
return previousValue;
}
}, []);
console.log(scoreArr); //[{score: 81}, {score: 92}, {score: 100}]
console.log(scoreArr1); //[{score: 81}, {score: 92}, {score: 100}]
5. 按順序執(zhí)行promise
function p1(num) {
return new Promise((rs, rj) => {
setTimeout(() => {
rs(num + 5);
}, 100);
});
};
function p2(num) {
return new Promise((rs, rj) => {
setTimeout(() => {
rs(num * 2);
}, 300);
});
};
function p3(num) {
return new Promise((rs, rj) => {
rs(num - 3);
})
};
//鏈?zhǔn)秸{(diào)用
Promise.resolve(10)
.then(p1)
.then(p2)
.then(p3)
.then(res => console.log(res)) ; //27
//使用reduce執(zhí)行promise
var arr = [p1, p2, p3];
var lastPromise = arr.reduce((previousPromise, currentPromise) => previousPromise.then(currentPromise), Promise.resolve(10));
lastPromise.then(res => console.log(res)); //276. 使用compose函數(shù)組合實(shí)現(xiàn)管道
管道(Pipe)是指輸入一個(gè)值,依次經(jīng)過(guò)管道(有序的函數(shù)運(yùn)算)后輸出這個(gè)值,是函數(shù)編程的核心思想
function add10(num) {
return num + 10;
};
function multipl2(num) {
return num * 2;
};
function divide3(num) {
return num / 3;
};
const compose = (fns) => (initialValue) => fns.reduce((previous, current) => current(previous), initialValue);
const calculate1 = compose([add10, divide3]);
const calculate2 = compose([divide3, add10, multipl2]);
console.log(calculate1(20)); //10
console.log(calculate2(9)); //26到此這篇關(guān)于JavaScript reduce方法使用方法介紹的文章就介紹到這了,更多相關(guān)JS reduce內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ES6新特性之函數(shù)的擴(kuò)展實(shí)例詳解
這篇文章主要介紹了ES6新特性之函數(shù)的擴(kuò)展,實(shí)例形式較為詳細(xì)的分析了ES6針對(duì)函數(shù)參數(shù)、運(yùn)算符及相關(guān)新特性的擴(kuò)展操作與注意事項(xiàng),需要的朋友可以參考下2017-04-04
JS module的導(dǎo)出和導(dǎo)入的實(shí)現(xiàn)代碼
這篇文章主要介紹了JS module的導(dǎo)出和導(dǎo)入的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
VsCode插件自動(dòng)生成注釋插件koroFileHeader使用教程
這篇文章主要介紹了VsCode插件自動(dòng)生成注釋插件koroFileHeader使用教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01
JavaScript實(shí)現(xiàn)帶粒子效果的進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)帶粒子效果的進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
js arguments.callee的應(yīng)用代碼
arguments.callee的使用方法,具體大家自己測(cè)試了2009-05-05
bootstrap下拉分頁(yè)樣式 帶跳轉(zhuǎn)頁(yè)碼
這篇文章主要為大家詳細(xì)介紹了bootstrap下拉分頁(yè)樣式,帶跳轉(zhuǎn)頁(yè)碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
利用js實(shí)現(xiàn)可進(jìn)行時(shí)間和工作調(diào)度的任務(wù)管理器
這篇文章主要為大家詳細(xì)介紹了如何利用js實(shí)現(xiàn)一個(gè)可進(jìn)行時(shí)間和工作調(diào)度的任務(wù)管理器,文中的示例代碼簡(jiǎn)潔易懂,有需要的小伙伴可以參考一下2023-10-10
Js為表單動(dòng)態(tài)添加節(jié)點(diǎn)內(nèi)容的方法
這篇文章主要介紹了Js為表單動(dòng)態(tài)添加節(jié)點(diǎn)內(nèi)容的方法,實(shí)例分析了js針對(duì)表單節(jié)點(diǎn)進(jìn)行添加操作的常用技巧,需要的朋友可以參考下2015-02-02
JavaScript使用performance實(shí)現(xiàn)查看內(nèi)存
這篇文章主要為大家詳細(xì)介紹了JavaScript如何使用performance實(shí)現(xiàn)查看內(nèi)存,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
???????基于el-table和el-pagination實(shí)現(xiàn)數(shù)據(jù)的分頁(yè)效果流程詳解
本文主要介紹了???????基于el-table和el-pagination實(shí)現(xiàn)數(shù)據(jù)的分頁(yè)效果,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-11-11

