
JavaScript Array Reduce Method
Basic Concepts
The reduce
method executes a callback function (the reducer) on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.
The syntax for reduce
is:
array.reduce(callback(accumulator, currentValue, [currentIndex], [array]), [initialValue])
callback
: A function to execute on each element in the array, taking four arguments:
accumulator
: The accumulator accumulates the callback’s return values. It is the accumulated value previously returned in the last invocation of the callback.currentValue
: The current element being processed in the array.currentIndex
(optional): The index of the current element being processed in the array.array
(optional): The arrayreduce
was called upon.
initialValue
(optional): A value to use as the first argument to the first call of the callback. If no initialValue is supplied, the first element in the array will be used as the initial accumulator value, and the first element will be skipped.
Example 1: Sum of an Array
Let’s use reduce
to calculate the sum of all elements in an array.
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) => {
return accumulator + currentValue;
}, 0);
console.log(sum); // Output: 15
Here’s what happens step-by-step:
- Initial value (
accumulator
) is0
. - First iteration:
accumulator = 0
,currentValue = 1
->accumulator + currentValue = 1
- Second iteration:
accumulator = 1
,currentValue = 2
->accumulator + currentValue = 3
- Third iteration:
accumulator = 3
,currentValue = 3
->accumulator + currentValue = 6
- Fourth iteration:
accumulator = 6
,currentValue = 4
->accumulator + currentValue = 10
- Fifth iteration:
accumulator = 10
,currentValue = 5
->accumulator + currentValue = 15
- Final result is
15
.
Example 2: Flattening an Array of Arrays
You can also use reduce
to flatten an array of arrays.
const arrays = [[1, 2], [3, 4], [5, 6]];
const flattened = arrays.reduce((accumulator, currentValue) => {
return accumulator.concat(currentValue);
}, []);
console.log(flattened); // Output: [1, 2, 3, 4, 5, 6]
Here’s what happens step-by-step:
- Initial value (
accumulator
) is[]
. - First iteration:
accumulator = []
,currentValue = [1, 2]
->accumulator.concat(currentValue) = [1, 2]
- Second iteration:
accumulator = [1, 2]
,currentValue = [3, 4]
->accumulator.concat(currentValue) = [1, 2, 3, 4]
- Third iteration:
accumulator = [1, 2, 3, 4]
,currentValue = [5, 6]
->accumulator.concat(currentValue) = [1, 2, 3, 4, 5, 6]
- Final result is
[1, 2, 3, 4, 5, 6]
.
Example 3: Counting Instances of Values
Another example is counting the instances of values in an array.
const names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];
const nameCount = names.reduce((accumulator, currentValue) => {
if (accumulator[currentValue]) {
accumulator[currentValue]++;
} else {
accumulator[currentValue] = 1;
}
return accumulator;
}, {});
console.log(nameCount); // Output: { Alice: 2, Bob: 1, Tiff: 1, Bruce: 1 }
Here’s what happens step-by-step:
- Initial value (
accumulator
) is{}
. - First iteration:
accumulator = {}
,currentValue = 'Alice'
->accumulator = { Alice: 1 }
- Second iteration:
accumulator = { Alice: 1 }
,currentValue = 'Bob'
->accumulator = { Alice: 1, Bob: 1 }
- Third iteration:
accumulator = { Alice: 1, Bob: 1 }
,currentValue = 'Tiff'
->accumulator = { Alice: 1, Bob: 1, Tiff: 1 }
- Fourth iteration:
accumulator = { Alice: 1, Bob: 1, Tiff: 1 }
,currentValue = 'Bruce'
->accumulator = { Alice: 1, Bob: 1, Tiff: 1, Bruce: 1 }
- Fifth iteration:
accumulator = { Alice: 1, Bob: 1, Tiff: 1, Bruce: 1 }
,currentValue = 'Alice'
->accumulator = { Alice: 2, Bob: 1, Tiff: 1, Bruce: 1 }
- Final result is
{ Alice: 2, Bob: 1, Tiff: 1, Bruce: 1 }
.
Conclusion
The reduce
method is versatile and can be used for a variety of tasks, such as summing values, flattening arrays, counting instances, and more. By understanding how to use reduce
, you can simplify complex data transformations