RxJS 学习系列 8. 过滤操作符 startWith,filter, last, first, skip, take

startWith: 在开头添加要发送的元素
startWith(an: Values): Observable
filter: 传入function 过滤发送的元素
take: 传入数字,只取N个数的元素
skip: 传入数字,跳过N个元素
last: 取最后一个元素
first: 取最后一个元素

<script src='https://cdn.bootcss.com/rxjs/6.5.1/rxjs.umd.js'></script>
<script>
   const { from } = rxjs;
   const { filter, take, last, startWith, skip } = rxjs.operators;

   // 发出(1, 2, 3, 4, 5)
   const source = from([1, 2, 3, 4, 5]);
   const example = source.pipe(
     // 开头追加 6, 8 得 6, 8, 1, 2, 3, 4, 5
     startWith(6, 8),
     // 舍弃第一个 得 8, 1, 2, 3, 4, 5
     skip(1),
     // 只取偶数得 8, 2, 4
     filter(num => num % 2 === 0),
     // 再取前俩得 8, 2
     take(2),
     // 只取最后一个得 2
     last()
   );
   example.subscribe(val => {
     console.log(`The number: ${val}`)
   });

</script>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。