Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
The reduce method is likely the most versatile method in javascript. This makes it a very powerful tool to use and mastering it will obviously be a great asset when using the language. I however noticed that most js developers struggle with this method (From no understanding to just a mere grasp of it). I used to struggle with this method too and seldomly used it since I did not have a proper understanding of the method. Thank God it’s not the case anymore and my objective through this article is that it isn’t the case for you too.
reduce(callbackFn, initialValue)
reduce(calllbackFn)
Reduce takes a callback function and an initial value. The callback method takes four optional parameters which are the accumulator, the current item, the current index and the array you are looping through.
A typical reduce call will look like this:myArray.reduce((accumulator, currentItem, currentIndex, array) => {
// Your logic here
}, initialValue);
Accumulator is initialized with initialValue, accumulator is passed in as an input into every iteration. its value is updated to the returned value of each iteration and the last is finally what is returned from the reduce method
We can see the count result is 3 which is correct
We can see that the sum of all three ages is 91.
The function above can be re-written as for better clarity.
In the first iteration, acc is initialized to zero so sum returns curr.age. The returned sum is assigned to acc, which is fed into the next iteration, so in the second sum is acc (previous age) + current age. Again this sum is returned so value is assigned to accumulator. In the 3rd iteration, acc’s value is the sum of the 2 previous ages and so the loop continues till the end.
I hope are beginning to get the logic. Let’s move to our next example.
The first implementation is a little more compact. We make use of the spread operator to populate the returned with object with acc‘s fields, adding to it a new field curr.id‘s value. As explained earlier, this newly returned object becomes acc new value in the next iteration.
The second implementation is clearer as we clearly see the assignation of curr to acc[curr.id].
Both implementations are valid, you can use the one you are more comfortable with.
We initialize max to the first person’s age and iterate over, if we find the someone with age > max, we return the age which becomes max‘s new value in the next iteration. Straight forward right !
The object with name PETER is returned successfully. Now let’s check with a name that doesn’t exist and see the result
As you can see we get null. Remember however to that the find method is more optimal for this use case.
We have our expected value. As we can see. Mary is 16. So the result is false. Not everyone is above 18. I’m leaving it to you to run the case where everyone is above 18 and see the results. Let’s move to the next.
Our results value is true since we have 2 people above 18 so the result is true. Like the example above I am leaving the false case for you to test 🙂
We provide two implementations above. The first implementation is a one line method and makes use of the spread operator. If you are not used to the spread operator, the second implementation is more explicit.
As we can see, acc is initialized to an empty object, in the callback function we check if the acc has the field curr.status (whose value could be ‘pending’, ‘shipping’ or ‘cancelled’). If it does not the field is added and it’s value set to 1 else the value is simply incremented
We are going to use recursion to solve this.
We have come to the end of this tutorial. Hope it was helpful and you feel more confident in using reduce in your projects.
I will like to say a big Thank you to @Leigh Halliday whose video made me understand reduce and which is the basis for this article.
Do not hesitate to write us in the comment section to ask your questions, we will be there to reply you
This is so so helpful thanks a lot. You are a gem !
Thank you
Great article !