O'Reilly logo

Mastering Reactive JavaScript by Erich de Souza Oliveira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The reduce() operator

The reduce() operator lets you run a function to accumulate all the values of an Observable to generate a new Observable containing only one value (the accumulated value).

This operator has the following signature:

observable.reduce(accumulatorFunction,[initialValue]); 

The first parameter is optional and the second one is mandatory:

  • accumulatorFunction: This is a function that is used to accumulate values from an observable. This function can receive up to four parameters:
    • acc: This is an accumulated value
    • currentValue: This is the value used in this iteration
    • currentIndex: This is a zero-based index of this iteration
    • source: This is the observable used
  • initialValue: This is the initial accumulator

We can use ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required