Home » RxJS elementAt() Filtering Operator

RxJS elementAt() Filtering Operator

by Online Tutorials Library

RxJS elementAt() Filtering Operator

RxJS elementAt() operator is a filtering operator that emits a single value in a sequence of emissions from the source Observable after giving the specified index.

In other words, we can say that the RxJS elementAt operator is used to fetch an item located at a specified index location in the sequence of items emitted by the source Observable. It emits only the i-th value, then completes.

Syntax:

Following is the syntax of the elementAt() operator:

Or

Parameter Explanation

  • index: It specifies a number i for the i-th source emission since the subscription started. This number starts from 0. The value from the source observable for this index will be given back.
  • defaultValue: This is an optional argument that specifies the default value returned for missing indices. Its default value is undefined.

Return value

The RxJS elementAt() operator’s return value is observable that emits a single item if it is found. Otherwise, it will emit the default value if it is given. If not, then it will emit an error.

Let us see an example of elementAt() operator to understand it clearly.

Example

Output:

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

RxJS elementAt() Filtering Operator

In the above example, you will see that ‘click 1’ and ‘click 2’ event will don’t emit any result and only ‘click 3’ event will emit the result as output. This is because we have set click at element At value at 2 and the RxJS elementAt() operator uses a 0 based index.


Next TopicRxJS Operators

You may also like