码迷,mamicode.com
首页 > Web开发 > 详细

[RxJS] Transformation operator: map and mapTo

时间:2016-05-24 22:32:45      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

We made our first operator called multiplyBy, which looks a bit useful, but in practice we don‘t need it because it‘s too specific: it only does simple multiplication on numbers. In this lesson we will see how the map() operator is useful for any calculation on delivered values.

 

map:

var foo = Rx.Observable.interval(1000);

/*

foo: ---0---1---2---3--...
       map(x => x / 2)
bar: ---0---2---4---6--...

*/

// map take a function
var bar = foo.map(x => x / 2);

bar.subscribe(
  function (x) { console.log(‘next ‘ + x); },
  function (err) { console.log(‘error ‘ + err); },
  function () { console.log(‘done‘); },
);

 

mapTo:

var foo = Rx.Observable.interval(1000);

/*

foo: --- 0---1--- 2--- 3--...
       mapTo(10)
bar: ---10---10---10---10--...

*/

// mapTo take a value
var bar = foo.mapTo(10);

bar.subscribe(
  function (x) { console.log(‘next ‘ + x); },
  function (err) { console.log(‘error ‘ + err); },
  function () { console.log(‘done‘); },
);

 

[RxJS] Transformation operator: map and mapTo

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5524725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!