Rx.Observable.prototype.reduce(accumulator, [seed])
Applies an accumulator function over an observable sequence, returning the result of the aggregation as a single element in the result sequence. The specified seed value is used as the initial accumulator value.
For aggregation behavior with incremental intermediate results, see the scan
method.
Arguments
accumulator
(Function
): An accumulator function to be invoked on each element with the following arguments:acc
:Any
- the accumulated value.currentValue
:Any
- the current valueindex
:Number
- the current indexsource
:Observable
- the current observable instance
[seed]
(Any
): The initial accumulator value.
Returns
(Observable
): An observable sequence containing a single element with the final accumulator value.
Example
/* With a seed */
var source = Rx.Observable.range(1, 3)
.reduce(function (acc, x, idx, source) {
return acc * x;
}, 1)
var subscription = source.subscribe(
function (x) {
console.log('Next: %s', x);
},
function (err) {
console.log('Error: %s', err);
},
function () {
console.log('Completed');
});
// => Next: 6
// => Completed
/* Without a seed */
var source = Rx.Observable.range(1, 3)
.reduce(function (acc, x, idx, source) {
return acc * x;
})
var subscription = source.subscribe(
function (x) {
console.log('Next: %s', x);
},
function (err) {
console.log('Error: %s', err);
},
function () {
console.log('Completed');
});
// => Next: 6
// => Completed
Location
File:
Dist:
Prerequisites:
NPM Packages:
NuGet Packages:
Unit Tests: