RxJS Reduce() Mathematical Operator
The RxJS reduce() operator is a mathematical operator that applies an accumulator function over the input or source Observable, which returns an accumulated value in an observable form when the source is completed. It also gives an optional seed value.
In simple words, we can say that the reduce() function or operator combines together all values emitted on the source, using an accumulator function. The accumulator function knows how to join a new source value into the accumulation from the past.
This function takes in 2 arguments, the first one is the accumulator function, and the second is the seed value.
Syntax:
Following is the syntax of the reduce() operator:
Parameter Explanation
- accumulator_func: It is used to specify a function called on each source values from the observables.
- seed: This is an optional value. It specifies an initial accumulation value. By default, it is undefined.
Return value
The return value of the reduce() operator is observable that gives a single accumulated value.
Let us see some examples of reduce() operator to understand it clearly.
Example
Output:
After executing the above example, you will see the following result:
Total Price of Products is: 23000
Here, you can see that it has combined all the products we have assigned.