ElementAt
emit only item n emitted by an Observable
The ElementAt
operator pulls an item located at a specified index location in the sequence of items emitted by the source Observable and emits that item as its own sole emission.
See Also
Language-Specific Information
RxGroovy elementAt elementAtOrDefault

RxGroovy implements this operator as elementAt
. Pass elementAt
a zero-based index value and it will emit the solitary item from the source Observable’s sequence that matches that index value (for example, if you pass the index value 5, elementAt
will emit the sixth item emitted by the source Observable).
If you pass in a negative index value, or if the source Observable emits fewer than index
value + 1
items, elementAt
will throw an IndexOutOfBoundsException
.
- Javadoc:
elementAt(int)

RxGroovy also implements the elementAtOrDefault
operator. It differs from elementAt
in that it will not throw an exception if the source Observable emits fewer than index value + 1
items. Instead, it will emit a “default” item that you specify with an additional parameter to elementAtOrDefault
.
If you pass in a negative index value, elementAt
will throw an IndexOutOfBoundsException
.
- Javadoc:
elementAtOrDefault(int,T)
elementAt
and elementAtOrDefault
do not by default operate on any particular Scheduler.
RxJava 1․x elementAt elementAtOrDefault

RxGroovy implements this operator as elementAt
. Pass elementAt
a zero-based index value and it will emit the solitary item from the source Observable’s sequence that matches that index value (for example, if you pass the index value 5, elementAt
will emit the sixth item emitted by the source Observable).
If you pass in a negative index value, or if the source Observable emits fewer than index
value + 1
items, elementAt
will throw an IndexOutOfBoundsException
.
- Javadoc:
elementAt(int)

RxGroovy also implements the elementAtOrDefault
operator. It differs from elementAt
in that it will not throw an exception if the source Observable emits fewer than index value + 1
items. Instead, it will emit a “default” item that you specify with an additional parameter to elementAtOrDefault
.
If you pass in a negative index value, elementAt
will throw an IndexOutOfBoundsException
.
- Javadoc:
elementAtOrDefault(int,T)
elementAt
and elementAtOrDefault
do not by default operate on any particular Scheduler.
RxJS elementAt

RxJS implements this operator as elementAt
. Pass elementAt
a zero-based index value and it will emit the solitary item from the source Observable’s sequence that matches that index value (for example, if you pass the index value 5, elementAt
will emit the sixth item emitted by the source Observable).
If there is no element in the source sequence with the index value you specify, elementAt
will issue an onError
notification: “Argument out of range
”
Sample Code
var source = Rx.Observable.fromArray([1,2,3,4]) .elementAt(1); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); });
Next: 2 Completed
You may optionally pass in a default value that elementAt
will emit if the source Observable emits no values:
Sample Code
var source = Rx.Observable.fromArray([]) .element({defaultValue: 23}); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); });
Next: 23 Completed
elementAt
is found in each of the following distributions:
rx.all.js
rx.all.compat.js
rx.aggregates.js
They require one of the following distributions:
rx.js
rx.compat.js
rx.lite.js
rx.lite.compat.js
© ReactiveX contributors
Licensed under the Apache License 2.0.
http://reactivex.io/documentation/operators/elementat