问题
I'm new to RxJS. I know I could just .filter
and .map
an observable to get the change I'm looking for. But, is there any method which combines the two into one function?
回答1:
Yes there is.
FlatMap.
Suppose you have an Observable of numbers (1, 2, 3, 4, 5, ...) and you want to filter for even numbers and map them to x*10.
var tenTimesEvenNumbers = numbers.flatMap(function (x) {
if (x % 2 === 0) {
return Rx.Observable.just(x * 10);
} else {
return Rx.Observable.empty();
}
});
来源:https://stackoverflow.com/questions/32446977/is-there-an-rx-method-which-combines-map-and-filter