Home » RxJS last() Filtering Operator

RxJS last() Filtering Operator

by Online Tutorials Library

RxJS last() Filtering Operator

RxJS last() operator is a filtering operator that emits only the last value or the last value that meets some specified condition emitted by the source observable.

Syntax:

Following is the syntax of the RxJS last() operator:

Or

Parameter Explanation

  • predicate: It is used to specify the condition that any source emitted item has to satisfy. It is an optional argument. Its default value is undefined.
  • defaultValue: It specifies if the last predicate isn’t met or no values were emitted. It is also an optional argument. Its default value is undefined.

Return value

The RxJS last() operator’s return value is observable that emits the last value.

Let us see some examples of the RxJS last() operator to understand it clearly.

Example 1 (Emit last value in a given sequence)

Output:

After executing the above example, you will see the following result:

RxJS last() Filtering Operator

Example 2 (Emit last value to meet a specific condition)

Output:

After executing the above example, you will see the following result:

RxJS last() Filtering Operator

In the above example, you can see that the last given value is 9, but the condition is to emit the last even number; that’s why it has emitted 8.

Example 3 (Emit Last with default value)

Output:

After executing the above example, you will see the following result:

RxJS last() Filtering Operator


Next TopicRxJS Operators

You may also like