do -> tap catch -> catchError switch -> switchAll finally -> finalize
mep 相似于 Array.prototype.map()
switchMap switchMap 会中止发出先前发出的内部 Observable 并开始发出新的内部 Observable 的值。(能够中止上一次发出的ajax)
mergeMap 将每一个值映射到Observable,必须返回一个Observablees6
reduce 只返回最后的值ajax
// res: 12, 15 from([2, 3]).pipe( scan((acc, item) => acc += item, 10)) .subscribe(v => console.log(v)) // res: 15 from([2, 3]).pipe( reduce((acc, item) => acc += item, 10)) .subscribe(v => console.log(v))
filter 返回你想要的数据
partition 返回两个 Observables [0] 符合断言, [1] 不符合断言api
from([2, 3, 4]).pipe( filter(item => item <= 3)) .subscribe(v => console.log(v))
将当前值和前一个值做为数组放在一块儿,而后将其发出数组
of(1, 2, 3) .pipe( pairwise()).subscribe(v => console.log(v)) [1,2] [2,3]
均可以接收一个函数做为参数promise
from([1,2]).pipe(max()).subscribe(l) // 2 from([1,2]).pipe(min()).subscribe(l) // 1 from([1,2]).pipe(count()).subscribe(l) // 2
过滤掉重复的项app
from([1, 2, 2, 3, 2]) .pipe(distinct()) .subscribe(l); // 1,2,3
只发出第n个值, 而后完成 ,从0开始async
from([1, 2]) .pipe(elementAt(0, "default value")) .subscribe(l);
忽略源所发送的全部项,只传递 complete 或 error函数
skip 跳过源发出的前N个值
skipLast 跳过源最后发出的的N个值
skipWhile 跳过lambda返回true的值ui
take 接收源 最初的N个值
takeLast 接收源最后N个值
takeUntil notifier发出值, 源断流
takeWhile lambda返回true,才发出源的值
先发出最新的值, 在忽略
先忽略, 在发出最新的值
interval(500) .pipe(auditTime(1000)) .subscribe(l); // 1s后发出 2, 1被忽略
延时发送源发出的值, 若是期间源发出了新值的话,返回的值为最新的值,上一个会被丢弃掉(避免高消费事件时使用)
在周期时间间隔内发出源最新值。
相似 Array.prototype.find()
把 Observable 转化为 promise
click = async e => { let res = await ajax('http://localhost:1995/a').pipe(map(res => res.response)).toPromise(); l(res) }
buffer系列,将过去的值做为数组收集,在事件触发时发出收集好的值
const send$= fromEvent(document, 'click'); const interval = interval(1000); const buffered = interval.pipe(buffer(send$)); buffered.subscribe(l);
若是源Observable完成而没有发出任何next值,则发出给定值 ,不然镜像源Observable
const { of, from, empty } = require("rxjs"); const { mergeMap, defaultIfEmpty } = require("rxjs/operators"); from([1, 2, 2, 3, 2]) .pipe( mergeMap(el => (el > 10 ? of(el) : empty())), defaultIfEmpty("not data"), ) .subscribe(l); // not data
延迟来自源Observable的项目的发射
from([1, 2]) .pipe(endWith("源观察完成后,附加一个发射,而后完成。")) .subscribe(l); // 1, 2, "源观察完成后,附加一个发射,而后完成。"
pluck(properties: ...string); 取一个对象的属性相似 obj.xxx.xxx toArray(); 把流发出的值塞在Array后,返回Array