Zach Olivare - 2023 Apr 17
And a practical guide to the reduce() function
There are three primary methods to iterate over an array in JavaScript: forEach
, map
, and reduce
. Each of these functions has its own use case, and it's important to understand the differences between them.
The forEach
method is the most basic of the three. It performs a simple iteration over an array, and doesn't return anything. As it's name implies, it is mostly equivalent to a for...of loop in JavaScript.
Use forEach
when:
array.forEach((item) => item.foo = 'bar')
DO NOT USE forEach
WHEN:
await
one or more promisesforEach
is synchronous, so it will not wait for the async function to finish before moving on to the next iteration (making the callback function async
will not help)map()
to generate an array of promises, and then pass that to Promise.all()The map
method transforms each element in the array. Its only argument is a callback function that takes the original array item and outputs a different array item.
map
returns a new array with the same length as the original array, but with each element transformed.
Use map
when:
DO NOT USE map
WHEN:
forEach
(or a for loop), not map
.The reduce
method takes an array and reduces it to a single value. That single value is usually either an object or a number. Reduce tends to confuse people more than the other two, but 99% of the time reduce
is used for one of two tasks.
Use reduce
when:
0
is passed as the final argument to reduce{}
is passed as the final argument to reduceThe signature of reduce
can also be intimidating because it usually has a big inline function, but without the inline function it just looks like this:
The callback
function is called once for each item in the array. It takes two arguments: accumulator
and currentValue
.
accumulator
: Remember before when we said that reduce()
reduces an array to a "single value"? That single value is the accumulator
. Every iteration of the callback function has to return something. Whatever you return from the callback will be passed in as the accumulator
for the next iteration of the callback function. That's how it "accumulates".
currentValue
is just the current item in the array. It's common to change the name of this argument to something more descriptive, specific to your situation.
First, here is the classic example of using reduce
to sum up an array of numbers:
But it's not often you have an array of numbers. It's usually an array of objects where each object has some numeric value that you want to sum up.
It looks a little more complicated this time because of the inline function, but the only difference is that instead of just a number, we have an object with a numeric value
property.
Finding an item in an array is an O(n) operation. If you have an array of 100 items, it could take 100 iterations to find the item you're looking for. For that reason, if you're going to be doing a lot of searching, it's common to transform an array into an object, indexed by your search value, so that you can find the value immediately (in constant, O(1), time), without going through the whole array.
Let's transform this array into this object, so that we can search for each item by it's id
.
And that can be done with reduce
like this: