Computes the sum of a sequence of values that are obtained by invoking an optional transform function on each element of the input sequence, else if not specified computes the sum on each item in the sequence.
[keySelector]
(Scheduler
): A transform function to apply to each element. The callback is called with the following information:- the value of the element
- the index of the element
- the Observable object being subscribed
(Observable
): An observable sequence containing a single element with the sum of the values in the source sequence.
/* Without a selector */
var source = Rx.Observable.range(1, 10)
.sum();
var subscription = source.subscribe(
function (x) {
console.log('Next: ' + x);
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
});
// => Next: 55
// => Completed
/* With a selector */
var array = [
{ value: 1 },
{ value: 2 },
{ value: 3 }
];
var source = Rx.Observable
.from(array)
.sum(function (x, idx, obs) {
return x.value;
});
var subscription = source.subscribe(
function (x) {
console.log('Next: ' + x);
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
});
// => Next: 6
// => Completed
File:
Dist:
Prerequisites:
NPM Packages:
NuGet Packages:
Unit Tests: