Making peace with the javascript Array.reduce() method.
Tags: javascript, es6, array
Published: 2020-02-25 21:22:42

Out of all the common array methods in Javascript, Array.reduce() is the one I have struggled the most with. Mainly because it is a method that takes many ‘obscure’ parameters and callback arguments. I will try to shed a light upon those in this post.

If I was to summarise what .reduce() does, I would say it is a nice and compact way to loop over an array to create a new value derived from all the values of the array.

A common simple application of this method is returning the sum, the minimum or the average of the values of an array. It can also be used for more complex operations as we'll see later in this article.

Let's have a look at a simple example. We want to return the sum of all the elements in an array: 

```javascript
const array = [ 0, 1, 2, 3 ]
const total = array.reduce(
  ( accumulator, currentValue ) => accumulator + currentValue,
  0
)
// total is 6
```

Which can also be written using the more traditional for-loop syntax:

```javascript
const array = [ 0, 1, 2, 3 ]
let total = 0;
for(let i = 0; i < array.length; i++) {
  total += array[i];
}
// total is 6
```

Let's dive into how to use this method. The syntax can seem a bit cryptic at first when looking at the definition: 

```javascript
array.reduce(reducerFunction(total, currentValue, currentIndex, arr), initialValue)
```
Let's explore what those parameters are: 

The __reducerFunction__ (to be applied sequentially to each element of the array) takes on __four arguments__:

__total (required)__ is also often called ‘accumulator’ as it is the accumulated result of the callback function being applied on each element of the array. It is remembered across each iteration, and ultimately becomes the final resulting value (which can also be an array or an object, as we'll see in the following example).

__currentValue (required)__ is the value of the current element of the array to which the reducer function is being applied. 

__index (optional)__ is the index of the element of the array to which the callback function is being applied.

__arr (optional)__ is a way to access the array on which the method is applied. You might need it for more complex logic, like when you need to know how much of the array you’ve already traversed (using arr.length).

You can also specify an __initValue (optional)__ after the reducer function. This initial value is the one from which the accumulator starts. If this value is not supplied, the accumulator starts from 0.

OK, now that we understand the theory, let's use .reduce() to remove duplicates from an array. For this example, I took inspiration from [this brilliant article](https://medium.com/dailyjs/how-to-remove-array-duplicates-in-es6-5daa8789641c) by Samantha Ming.

This is our initial array. It contains duplicate values and we want to make it an array of single values.  

```javascript
const array = [0, 4, 0, 5, 6, 6, 10]
```

In this example our 'total', the final value to be returned will be the array of single values we want as a result (we call it finalArray). The initial value of the 'total' is an empty array.

```javascript
const arrayOfUnique = array.reduce(function(finalArray, currentValue){
    finalArray.includes(currentValue) ? 
    null :
    finalArray.push(currentValue)
    return finalArray
}, [])
// arrayOfUnique is [0, 4, 5, 6, 10]
```

At every iteration, our function checks if the currentValue is already present in the finalArray. If it isn't, it adds it to the finalArray. When we are finished iterating the function on every value in the array, our finalArray contains only single values.