reduce()
方法对数组的每个元素执行一个归约函数,并返回一个单独的输出值。
示例
const message = ["JavaScript ", "is ", "fun."];
// function to join each string elements
function joinStrings(accumulator, currentValue) {
return accumulator + currentValue;
}
// reduce join each element of the string
let joinedString = message.reduce(joinStrings);
console.log(joinedString);
// Output: JavaScript is fun.
reduce() 语法
reduce()
方法的语法是
arr.reduce(callback(accumulator, currentValue), initialValue)
这里,arr 是一个数组。
reduce() 参数
reduce()
方法接受
- callback - 对每个数组元素执行的回调函数(如果未提供initialValue,则跳过第一个元素)。它接受:
- accumulator - 累积回调函数的返回值。
- currentValue - 从数组中传递的当前元素。
- initialValue(可选)- 在第一次调用时传递给
callback()
的值。如果未提供,第一个元素将在第一次调用时充当accumulator,并且callback()
不会在第一个元素上执行。
注意:在没有initialValue的情况下对空数组调用reduce()
会抛出TypeError
。
reduce() 返回值
- 返回归约数组后的单个值。
注意事项:
reduce()
从左到右为每个值执行给定的函数。reduce()
不会改变原始数组。- 提供
initialValue
几乎总是更安全。
示例 1:计算数组所有值的总和
const numbers = [1, 2, 3, 4, 5, 6];
function sum_reducer(accumulator, currentValue) {
return accumulator + currentValue;
}
let sum = numbers.reduce(sum_reducer);
console.log(sum); // 21
// using arrow function
let summation = numbers.reduce(
(accumulator, currentValue) => accumulator + currentValue
);
console.log(summation); // 21
输出
21 21
示例 2:在数组中减去数字
const numbers = [1800, 50, 300, 20, 100];
// subtract all numbers from first number
// since 1st element is called as accumulator rather than currentValue
// 1800 - 50 - 300 - 20 - 100
let difference = numbers.reduce(
(accumulator, currentValue) => accumulator - currentValue
);
console.log(difference); // 1330
const expenses = [1800, 2000, 3000, 5000, 500];
const salary = 15000;
// function that subtracts all array elements from given number
// 15000 - 1800 - 2000 - 3000 - 5000 - 500
let remaining = expenses.reduce(
(accumulator, currentValue) => accumulator - currentValue,
salary
);
console.log(remaining); // 2700
输出
1330 2700
本示例清楚地解释了传递initialValue和不传递initialValue之间的区别。
示例 3:从数组中删除重复项
let ageGroup = [18, 21, 1, 1, 51, 18, 21, 5, 18, 7, 10];
let uniqueAgeGroup = ageGroup.reduce(function (accumulator, currentValue) {
if (accumulator.indexOf(currentValue) === -1) {
accumulator.push(currentValue);
}
return accumulator;
}, []);
console.log(uniqueAgeGroup); // [ 18, 21, 1, 51, 5, 7, 10 ]
输出
[ 18, 21, 1, 51, 5, 7, 10 ]
示例 4:按属性对对象进行分组
let people = [
{ name: "John", age: 21 },
{ name: "Oliver", age: 55 },
{ name: "Michael", age: 55 },
{ name: "Dwight", age: 19 },
{ name: "Oscar", age: 21 },
{ name: "Kevin", age: 55 },
];
function groupBy(objectArray, property) {
return objectArray.reduce(function (accumulator, currentObject) {
let key = currentObject[property];
if (!accumulator[key]) {
accumulator[key] = [];
}
accumulator[key].push(currentObject);
return accumulator;
}, {});
}
let groupedPeople = groupBy(people, "age");
console.log(groupedPeople);
输出
{ '19': [ { name: 'Dwight', age: 19 } ], '21': [ { name: 'John', age: 21 }, { name: 'Oscar', age: 21 } ], '55': [ { name: 'Oliver', age: 55 }, { name: 'Michael', age: 55 }, { name: 'Kevin', age: 55 } ] }
另请阅读