数组方法reduce

Last updated on January 20, 2023 pm

reduce(以下案例摘自mdn)

1
2
3
4
5
6
7
8
9
10
11
const array1 = [1, 2, 3, 4];

// 0 + 1 + 2 + 3 + 4
const initialValue = 0;
const sumWithInitial = array1.reduce(
(accumulator, currentValue) => accumulator + currentValue,
initialValue
);

console.log(sumWithInitial);
// Expected output: 10

笔记

1
2
3
4
5
//第一个参数为回调,第二个参数为初始值,第二个为可选参数
//1、无初始值
//accumulator为数组第一项,从第二项开始遍历,返回值会传到下一次回调中
//2、有初始值
//accumulator为设置的初始值,从第一项开始遍历,返回值会传到下一次回调中

数组方法reduce
https://angelaggression.github.io/2023/01/20/数组方法reduce/
Author
BurnedWings
Posted on
January 20, 2023
Licensed under