[RxJS] Transformation operator: map and mapTo

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[RxJS] Transformation operator: map and mapTo相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

[RxJS] Transformation operator: buffer, bufferCount, bufferTime

[RxJS] Transformation operator: map and mapTo

[RxJS] Transformation operators: delay and delayWhen

[RxJS] Transformation operator: repeat

kettle中跑transformation和job的区别

CG中的仿射(affine transformation)和法线变换