Skip to content

Latest commit

 

History

History
80 lines (63 loc) · 2.63 KB

average.md

File metadata and controls

80 lines (63 loc) · 2.63 KB

Rx.Observable.prototype.average([selector], [thisArg])

Computes the average of an observable sequence of values that are in the sequence or obtained by invoking a transform function on each element of the input sequence if present.

Arguments

  1. [selector] (Function): A transform function to apply to each element.
  2. [thisArg] (Any): Object to use as this when executing selector.

Returns

(Observable): An observable sequence containing a single element with the average of the sequence of values.

Example

// Without a selector
var source = Rx.Observable.range(0, 9).average();

var subscription = source.subscribe(
    function (x) {
        console.log('Next: ' + x);
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => Next: 4
// => Completed

// With a selector
var arr = [
    { value: 1 },
    { value: 2 },
    { value: 3 }
];

var source = Rx.Observable.from(arr).average(function (x) {
    return x.value;
});

var subscription = source.subscribe(
    function (x) {
        console.log('Next: ' + x);
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => Next: 2
// => Completed

Location

File:

Dist:

Prerequisites:

NPM Packages:

NuGet Packages:

Unit Tests: