O'Reilly logo

Mastering Reactive JavaScript by Erich de Souza Oliveira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The map() operator versus the flatMap() operator

These two operators are probably the most important and most used operators from functional reactive programming, and for this reason it is really important to know how to differentiate them.

The map() operator lets you pass as a parameter a function that receives as input the data from the observable and returns new data that will be propagated instead. This new data can be of any type. You can see the map() operator in the following example:

Rx.Observable   .of(1,2,3)   .map((i)=>i+1)   .subscribe((i)=>console.log(i)); 

This code is really simple and only sums one to each element of the observable and logs them to the console, so running it you will see this output:

    2    3    4

You can see a representation ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required