What is a real world application of `.reduce()`?

What would be a real world example for using this method. I understand what its doing but it I guess i’m trying to picture what I’d use it for.

For a lot of these excessive I like to think about how i can apply to a real world application

2 Likes

Explanation of javascript’s reduce() with a real world use case

As we see, this method is not just for numeric computation.

This will seem a silly way to compute factorial…

[1,2,3,4,5,6,7].reduce((a, b) => a * b)    // 5040

but it is short and sweet. And this would seem a strange way to join strings…

array = ['A', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog'];
array.reduce((a, b) => a + ' ' + b)
// "A quick brown fox jumps over the lazy dog"

Neither is very real world when compared with the example given in the article. Perhaps in your reading you will find some other examples. Please do share!

2 Likes

Accumulator acts as a running total.
I think it would be helpful to calculate the cumulative sums of sales metric for each day.
For exmaple , sales_jan = [200,300,400,500, 100].
Day 1: $200
Day 2: $500
Day 3: $900
and so on…

7 Likes