简体   繁体   中英

Angular2 RxJs Observables: filter vs map?

Both the Filter and Map rxjs functions seem to be ways to manipulate a rxjs Observable 's stream. After tinking with them and from examples I've seen, they both seem to do the same thing.

What's the difference and when should I use one over the other?

  • Filter: Removes emitted data from the stream.
  • Map: Transforms it.

They do the exact same as the corresponding Array methods.

Eg

const stream = Observable.of([1,2,3,4,5]);

stream
  .map(x => x * 2)
  .subscribe(x => console.log(x)); // 2,4,6,8,10

stream
  .filter(x => x > 3)
  .subscribe(x => console.log(x)); // 4,5

Maybe this site helps to understand the difference: https://www.learnrxjs.io/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM