Javascript.reduce()
The reduce function in javascript is very powerful.
Set the seed to what you need!
Reduce is a native array method. The method cycles through an array and invokes a callback function on each element in the array and consolidates the value from each iteration to the seed/accumulator.
But what does it all mean? Let’s see the function and break it down a bit.
reduce = function(array, iterator, accumulator) {
let firstIteration = true;
for (let i = 0; i < array.length; i++) {
if (first === true && accumulator === undefined) {
accumulator = array[i];
} else {
accumulator = iterator(accumulator, array[i], i, array);
}
firstIteration = false;
}
return accumulator;
};The reduce function takes 3 arguments
Collection
- The set of data we are reducing
Iterator/Callback Function
- A function iterated over each element in the array
- The callback function accepts up to 4 arguments
Accumulator/Seed (Optional)
- It accumulates the value of each iteration
- The value is returned once the function is invoked
- If an accumulator is not provided at invocation, the first element is assigned as the accumulator
The boolean variable is established to determine if the accumulator is given and avoid any undefined elements in an array.
We loop through the array assigning the seed to the value of the iteration on the seed, element, index, and array.
Get Josh Nguyen’s stories in your inbox
Join Medium for free to get updates from this writer.
Callback Function/Iterator accepts 4 arguments
- Accumulator/seed — is the initial value or previous value in the function
- Element — is the current value that will affect the seed (previous value)
- Index (Optional) — is the index of the array or the key of an object that may or may not be relevant depending on the iterator
- Array (Optional) — is the set of data being reduced
Now that we have a basic understanding of the function along with the arguments it accepts, lets apply to a few examples to see how exactly this function works.
The most common example of reduce is applying the function to an array of numbers and returning the sum.
const array = [23, 4, 68, 2, 1, 56];const sum = reduce(array, (accu, num) => {
return accu + num;
}, 0);
// the seed is 0, we want a numeric value returned// returns 154
The function loops through the array and adds each value to the seed before returning the final value. Below we apply this to strings.
const array = ['This', 'is', 'a', 'string'];const sentence = reduce(array, (accu, strings) => {
return accu + strings + ' ';
},'');
// the seed is an empty string, we want a string returned// returns "this is a string"
How about arrays?
const array = [['This'], ['is'], ['an'], ['array']];const arr = reduce(array, (accu, arrays) => {
return accu.concat(arrays);
}, []);
// see is an array, we want an array returned// returns ['this', 'is', 'an', 'array']
Remember, set the seed to what you need. Finally, lets create an object counting how many of each color is in the array.
const array = ['green', 'blue', 'green', 'red', 'red', 'green', 'blue', 'green', 'green', 'red', 'blue', 'green', 'green'];const obj = reduce(array, (accu, color) => {
if (!accu[color]) {
accu[color] = 1;
} else {
accu[color] = accu[color]++;
}
return accu;
}, {});// returns { green: 7, blue: 3, red: 3 }
In Conclusion
Reduce is the Swiss army knife of High-Order Functions (HOF). It can even be used to write other HOF like filter, map, contains, etc. I hope this helps make your future in coding a bit easier. Just remember…
Set the seed to what you need.






