The findIndex
operator in RxJS takes as its parameter a predicate function. It returns
an Observable that emits either a single number — the zero-based index of the first item in the
source Observable sequence that matches the predicate — or −1
if no such item
matches.
The predicate function takes three parameters:
- the item emitted by the source Observable
- the zero-based index of that item
- the source Observable itself
You can also pass an object to findIndex
as an optional second parameter, and that object
will be available to the predicate function as “this
”.
Sample Code
/* Found an element */
var array = [1,2,3,4];
var source = Rx.Observable.fromArray(array)
.findIndex(function (x, i, obs) {
return x === 1;
});
var subscription = source.subscribe(
function (x) { console.log('Next: ' + x); },
function (err) { console.log('Error: ' + err); },
function () { console.log('Completed'); });
/* Not found */
var array = [1,2,3,4];
var source = Rx.Observable.fromArray(array)
.findIndex(function (x, i, obs) {
return x === 5;
});
var subscription = source.subscribe(
function (x) { console.log('Next: ' + x); },
function (err) { console.log('Error: ' + err); },
function () { console.log('Completed'); });
findIndex
is found in the following distributions:
rx.all.js
rx.all.compat.js
rx.aggregates.js
It requires one of the following distributions:
rx.js
rx.compat.js
rx.lite.js
rx.lite.compat.js