Contains

determine whether an Observable emits a particular item or not

Pass the Contains operator a particular item, and the Observable it returns will emit true if that item is emitted by the source Observable, or false if the source Observable terminates without emitting that item.

A related operator, IsEmpty returns an Observable that emits true if and only if the source Observable completes without emitting any items. It emits false if the source Observable emits an item.

See Also

Language-Specific Information:

contains

RxGroovy implements this operator as contains. It does not by default operate on any particular Scheduler.

exists

RxGroovy also implements the exists operator. It is similar to contains but tests items emitted by the source Observable against a predicate function you supply, rather than testing them for identity with a particular object. The Observable returned from exists will return true if the source Observable emits an item that satisfies your predicate function, and false if it completes without emitting such an item.

It does not by default operate on any particular Scheduler.

isEmpty

RxGroovy also implements the isEmpty operator. The Observable returned from isEmpty will return false if the source Observable emits an item, and true if it completes without emitting an item.

It does not by default operate on any particular Scheduler.

contains

RxJava implements this operator as contains. It does not by default operate on any particular Scheduler.

exists

RxJava also implements the exists operator. It is similar to contains but tests items emitted by the source Observable against a predicate function you supply, rather than testing them for identity with a particular object. The Observable returned from exists will return true if the source Observable emits an item that satisfies your predicate function, and false if it completes without emitting such an item.

It does not by default operate on any particular Scheduler.

isEmpty

RxJava also implements the isEmpty operator. The Observable returned from isEmpty will return false if the source Observable emits an item, and true if it completes without emitting an item.

It does not by default operate on any particular Scheduler.

contains

The contains operator in RxJS takes an optional second parameter: a zero-based index into the source Observable’s sequence at which to start searching for the item.

Sample Code

/* Without an index */
var source = Rx.Observable.of(42)
  .contains(42);

var subscription = source.subscribe(
  function (x) { console.log('Next: %s', x); },
  function (err) { console.log('Error: %s', err); },
  function () { console.log('Completed'); });
Next: true
Completed
/* With an index */
var source = Rx.Observable.of(1,2,3)
  .contains(2, 1);

var subscription = source.subscribe(
  function (x) { console.log('Next: %s', x); },
  function (err) { console.log('Error: %s', err); },
  function () { console.log('Completed'); });
Next: true
Completed

contains 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
indexOf

The indexOf operator in RxJS is similar to contains but rather than returning an Observable that emits true or false it returns an Observable that emits the index of the item in the source Observable sequence, or −1 if no such item was emitted.

The indexOf operator takes an optional second parameter: a zero-based index into the source Observable’s sequence at which to start searching for the item. The index value that the resulting Observable emits will be relative to this start point, not to the beginning of the sequence.

Sample Code

/* Without an index */
var source = Rx.Observable.of(42)
  .indexOf(42);

var subscription = source.subscribe(
  function (x) { console.log('Next: %s', x); },
  function (err) { console.log('Error: %s', err); },
  function () { console.log('Completed'); });
Next: 0 
Completed
/* With an index */
var source = Rx.Observable.of(1,2,3)
  .indexOf(2, 1);

var subscription = source.subscribe(
  function (x) { console.log('Next: %s', x); },
  function (err) { console.log('Error: %s', err); },
  function () { console.log('Completed'); });
Next: 0 
Completed

indexOf 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
findIndex

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'); });
Next: 0
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'); });
Next: -1
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
isEmpty

RxJS also implements the isEmpty operator. The Observable returned from isEmpty will return false if the source Observable emits an item, and true if it completes without emitting an item.

Sample Code

/* Not empty */
var source = Rx.Observable.range(0, 5)
    .isEmpty()

var subscription = source.subscribe(
    function (x) { console.log('Next: ' + x); },
    function (err) { console.log('Error: ' + err); },
    function () { console.log('Completed'); });
Next: false
Completed
/* Empty */
var source = Rx.Observable.empty()
    .isEmpty()

var subscription = source.subscribe(
    function (x) { console.log('Next: ' + x); },
    function (err) { console.log('Error: ' + err); },
    function () { console.log('Completed'); });
Next: true
Completed

isEmpty 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

RxPHP implements this operator as isEmpty.

If the source Observable is empty it returns an Observable that emits true, otherwise it emits false.

Sample Code

//from https://github.com/ReactiveX/RxPHP/blob/master/demo/isEmpty/isEmpty.php

$source = \Rx\Observable::emptyObservable()
    ->isEmpty();

$source->subscribe($stdoutObserver);

   
Next value: 1
Complete!
    
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/isEmpty/isEmpty-false.php

$source = \Rx\Observable::just(1)
    ->isEmpty();

$source->subscribe($stdoutObserver);

   
Next value: 0
Complete!