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
- FlatMap
- Introduction to Rx: Select
- RxMarbles:
map
- 101 Rx Samples: Select — Indexed
- Using the map method with Observable by Jafar Husain (JavaScript Video Tutorial)
Language-Specific Information
RxGroovy byLine cast encode 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.
- Javadoc:
map(Func1)

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.
- Javadoc:
cast(Class)

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.

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.
RxJava 1․x byLine cast encode map

RxJava implements this operator as map
.
This operator does not by default operate on any particular Scheduler.
- Javadoc:
map(Func1)

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.
- Javadoc:
cast(Class)

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.

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.
RxJS map pluck select

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:
- the emitted item
- the index of that item in the sequence of emitted items
- 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

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
-
Rx.helpers.pluck(property)
from Dennis Stoyanov’s RxJS book
RxPHP map mapWithIndex mapTo select pluck
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
RxPHP also has an operator pluck
.
Returns an Observable containing the value of a specified array index (if array) or property (if object) from all elements in the Observable sequence. If a property can't be resolved the observable will error.
Sample Code
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/pluck/pluck.php $source = Rx\Observable::fromArray([ (object)['value' => 0], (object)['value' => 1], (object)['value' => 2] ]) ->pluck('value'); $subscription = $source->subscribe($stdoutObserver);
Next value: 0 Next value: 1 Next value: 2 Complete!
© ReactiveX contributors
Licensed under the Apache License 2.0.
http://reactivex.io/documentation/operators/map.html