Skip to content

Latest commit

 

History

History
97 lines (78 loc) · 3.18 KB

lastordefault.md

File metadata and controls

97 lines (78 loc) · 3.18 KB

Rx.Observable.prototype.lastOrDefault([predicate], [defaultValue], [thisArg])

Returns the last element of an observable sequence that satisfies the condition in the predicate if specified, else the last element.

Arguments

  1. predicate (Function): A predicate function to evaluate for elements in the source sequence. The callback is called with the following information:
    1. the value of the element
    2. the index of the element
    3. the Observable object being subscribed
  2. [defaultValue] (Any): The default value if no such element exists. If not specified, defaults to null.
  3. [thisArg] (Any): Object to use as this when executing the predicate.

Returns

(Observable): Sequence containing the last element in the observable sequence that satisfies the condition in the predicate.

Example

/* No Match */
var source = Rx.Observable.empty()
    .lastOrDefault(null, 0);

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

// => Next: 0
// => Completed

/* Without predicate */
var source = Rx.Observable.range(0, 10)
    .lastOrDefault();

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

// => Next: 9
// => Completed

/* With predicate */
var source = Rx.Observable.range(0, 10)
    .lastOrDefault(function (x, idx, obs) {
        return x % 2 === 0;
    });

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

// => Next: 8
// => Completed

Location

File:

Dist:

Prerequisites:

NPM Packages:

NuGet Packages:

Unit Tests: