JavaScript Array Iterator Methods
.reduce()
PURPOSE: Reduce an array into a single value. Note that the “single value” can be a single object, array, or string.
reduce() is unique from the other array iterator methods we’ve seen so far in that it has a more complex callback function syntax:
The function is called with the following arguments:
array.reduce((accumulator, currentValue, currentIndex, array) => { function body }, initialValue);
-
accumulator- The value returned from the previous callback function. On the first call, this value is set toinitialValue. The accumulator argument is crucial - it is howreduce()is able to keep track of a value throughout the entire iteration process. -
currentValue- The value of the current element. This operates the same as theelementargument in other array iterator methods likemapandfilter. -
currentIndex(optional) - The index position of thecurrentValue. -
array(optional) - The arrayreduce()was called on.
Sum up the numbers in an array
const nums = [25, 5, 100, 10];
let sum = nums.reduce(function(accumulator, num) {
return accumulator + num
}, 0);
// sum equals 140
/*--- using an arrow function for the callback ---*/
let sum = nums.reduce((acc, num) => acc + num, 0);
Count votes
let votes = ['Yes', 'No', 'Yes', 'Yes', 'No'];
let tally = votes.reduce(function(acc, vote){
if(acc[vote]){
acc[vote] = acc[vote] + 1;
} else {
acc[vote] = 1;
}
return acc;
}, {});
// tally is { 'Yes': 3, 'No': 2 }
Let’s break this example down together.
reduce()is called on the votes array.- It takes two arguments:
acc(the accumulator) andvote(the current array element). accstarts as{}and eventually becomes the tally object.- The function checks if
acc[vote]exists.acc[vote]refers to the property of acc whose key is the current vote (‘Yes’ or ‘No’). - If
acc[vote]exists, it means this vote type has been encountered before, so the function increments the count:acc[vote] = acc[vote] + 1. - If
acc[vote]does not exist (which is true the first time a particular vote type is encountered), it setsacc[vote] = 1. - After modifying acc, the function returns it. This returned value becomes the acc for the next iteration.
- After
reduce()has processed all elements in the votes array, the final value of acc is assigned totally. tallyis an object that contains the count of each vote type: { ‘Yes’: 3, ‘No’: 2 }.
While it is true that reduce COULD be used to handle the same tasks as almost all of the other examples you’ve seen in this module, best practice is to stay focused on what reduce truly excels at, which is reducing an array down to a single value - an object, a number, a string, etc.