Map

transform the items emitted by an Observable by applying a function to each item

The Map operator applies a function of your choosing to each item emitted by the source Observable, and returns an Observable that emits the results of these function applications.

See Also

Language-Specific Information:

map

RxGroovy implements this operator as map. For example, the following code maps a function that squares the incoming value onto the values in numbers:

Sample Code

numbers = Observable.from([1, 2, 3, 4, 5]);

numbers.map({it * it}).subscribe(
  { println(it); },                          // onNext
  { println("Error: " + it.getMessage()); }, // onError
  { println("Sequence complete"); }          // onCompleted
);
1
4
9
16
25
Sequence complete

This operator does not by default operate on any particular Scheduler.

cast

The cast operator is a specialized version of Map that transforms each item from the source Observable by casting it into a particular Class before reemitting it.

encode

In the StringObservable class that is not part of the RxGroovy core there is also a specialty mapping operator, encode, that transforms an Observable that emits strings into an Observable that emits byte arrays that respect character boundaries of multibyte characters in the original strings.

byLine

Also in the StringObservable class that is not part of the RxGroovy core there is a specialty mapping operator called byLine, that transforms an Observable that emits strings into an Observable that emits lines of text, by buffering the strings from the source Observable until a line-feed is found in one of them.

map

RxJava implements this operator as map.

This operator does not by default operate on any particular Scheduler.

cast

The cast operator is a specialized version of Map that transforms each item from the source Observable by casting it into a particular Class before reemitting it.

encode

In the StringObservable class that is not part of the RxJava core there is also a specialty mapping operator, encode, that transforms an Observable that emits strings into an Observable that emits byte arrays that respect character boundaries of multibyte characters in the original strings.

byLine

Also in the StringObservable class that is not part of the RxJava core there is a specialty mapping operator called byLine, that transforms an Observable that emits strings into an Observable that emits lines of text, by buffering the strings from the source Observable until a line-feed is found in one of them.

map

RxJS implements this operator as map or select (the two are synonymous). In addition to the transforming function, you may pass this operator an optional second parameter that will become the “this” context in which the transforming function will execute.

The transforming function gets three parameters:

  1. the emitted item
  2. the index of that item in the sequence of emitted items
  3. the Observable from which that item was emitted

Sample Code

// Using a value
var md = Rx.Observable.fromEvent(document, 'mousedown').map(true);
var mu = Rx.Observable.fromEvent(document, 'mouseup').map(false);

// Using a function
var source = Rx.Observable.range(1, 3)
    .select(function (x, idx, obs) {
        return x * x;
    });

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

There is also an operator called pluck which is a simpler version of this operator. It transforms the elements emitted by the source Observable by extracting a single named property from those elements and emitting that property in their place.

Sample Code

var source = Rx.Observable
    .fromArray([
        { value: 0 },
        { value: 1 },
        { value: 2 }
    ])
    .pluck('value');

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

map/select and pluck are found in each of the following distributions:

  • rx.js
  • rx.all.js
  • rx.all.compat.js
  • rx.compat.js
  • rx.lite.js
  • rx.lite.compat.js

See Also

RxPHP implements this operator as map.

Takes a transforming function that operates on each element.

Sample Code

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

$observable = \Rx\Observable::fromArray([21, 42]);
$observable
    ->map(function ($elem) {
        return $elem * 2;
    })
    ->subscribe($stdoutObserver);

   
Next value: 42
Next value: 84
Complete!
    

RxPHP also has an operator mapWithIndex.

Maps operator variant that calls the map selector with the index and value

Sample Code

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

$subscriptions = Rx\Observable::fromArray([21, 42])
    ->mapWithIndex(function ($index, $elem) {
        return $index + $elem;
    })
    ->subscribe($stdoutObserver);

   
Next value: 21
Next value: 43
Complete!
    

RxPHP also has an operator mapTo.

Maps every value to the same value every time

Sample Code

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

$subscription = Rx\Observable::fromArray([21, 42])
    ->mapTo(1)
    ->subscribe($stdoutObserver);

   
Next value: 1
Next value: 1
Complete!
    

RxPHP also has an operator select.

Alias for Map