Reduce
Learn how 'reduce' enables us to convert a collection into a single value.
We'll cover the following
Array.reduce
This is the hardest array function among map
, filter
, and forEach
. Read this lesson multiple times if necessary. This isn’t easy to get the hang of.
reduce
is essentially a version of forEach
that is meant to be a pure function.
We’re doing something with each item in the array, but we’re not necessarily being returned an array. We’re also not supposed to be affecting the outer scope at all.
The purpose of reduce
is to turn an array of items into a single value. We’re going to start with a beginning value and each item in the array should transform our value in some way. The end result after all items have transformed our value is what we get back from the function.
The arguments that reduce
gives our callback are a little different than map
, filter
, and forEach
. We also have the option of giving it an extra parameter. Let’s show an example and then discuss it.
Get hands-on with 1400+ tech skills courses.