RxJS - Filtering Operator elementAt



This operator will give a single value from the source observable based upon the index given.

Syntax

elementAt(index: number): Observable

Parameters

index − The argument passed is the index of type number, starting from 0. The value from the source observable for this index will be given back.

Return value

An observable will be returned with a value based on the index given.

Example

import { fromEvent} from 'rxjs';
import { elementAt } from 'rxjs/operators';

let btn = document.getElementById("btnclick");
let btn_clicks = fromEvent(btn, 'click');
let case1 = btn_clicks.pipe(elementAt(4));
case1.subscribe(x => console.log(x));

We have used elementAt(4), so the 5th click will be emitted as the index starts from 0.

Output

elementAt Operator
Advertisements