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.
map
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
:
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 );
This operator does not by default operate on any particular Scheduler.
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.
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.
byLine cast encode map
RxJava implements this operator as map
.
This operator does not by default operate on any particular Scheduler.
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.
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.
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:
// 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'); });
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.
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'); });
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
Rx.helpers.pluck(property)
from Dennis Stoyanov’s RxJS bookmap mapWithIndex mapTo select pluck
RxPHP implements this operator as map
.
Takes a transforming function that operates on each element.
//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);
RxPHP also has an operator mapWithIndex
.
Maps operator variant that calls the map selector with the index and value
//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);
RxPHP also has an operator mapTo
.
Maps every value to the same value every time
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/map/mapTo.php $subscription = Rx\Observable::fromArray([21, 42]) ->mapTo(1) ->subscribe($stdoutObserver);
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.
//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);
© ReactiveX contributors
Licensed under the Apache License 2.0.
http://reactivex.io/documentation/operators/map.html