Learn RxJS
Search…
reduce

signature: reduce(accumulator: function, seed: any): Observable

Reduces the values from source observable to a single value that's emitted when the source completes.

πŸ’‘ Just like Array.prototype.reduce()​
πŸ’‘ If you need the current accumulated value on each emission, try scan!
​​
​
​

Examples

Example 1: Sum a stream of numbers
1
// RxJS v6+
2
import { of } from 'rxjs';
3
import { reduce } from 'rxjs/operators';
4
​
5
const source = of(1, 2, 3, 4);
6
const example = source.pipe(reduce((acc, val) => acc + val));
7
//output: Sum: 10'
8
const subscribe = example.subscribe(val => console.log('Sum:', val));
Copied!

Additional Resources

Last modified 1yr ago