LiveLoveApp logo

Transformation Operators

Transformation Operators

  • Transform values emitted from the source observable
  • Some work on higher order observables
  • The map operator is a transformation operator!

The scan Operator

  • Similar to Array#reduce()
  • Evaluate each item emitted from the source observable while maintaining an accumulated value
numbers$.pipe(scan((total, next) => total + next, 0)).subscribe();

Higher-Order Observables

clicks$.pipe(map(() => timer(1_000)));
  • Note that the type of is now Observable<Observable<number>>
  • This is called a higher-order observable
  • It's an observable of observables!
clicks$.pipe(
  map(() => timer(1_000)),
  mergeAll()
);
  • The mergeAll operator "flattens" the observable
  • It subscribes to each observable emitted by the map function

The mergeMap Operator

  • Short hand for map+mergeAll
  • Map items from the source observable to new observables, then flatten the result
click$.pipe(mergeMap(() => ajax('/api/v1/books.json')));