RxJS 學(xué)習(xí)系列 8. 過(guò)濾操作符 startWith,filter, last, first, skip, take

startWith: 在開(kāi)頭添加要發(fā)送的元素
startWith(an: Values): Observable
filter: 傳入function 過(guò)濾發(fā)送的元素
take: 傳入數(shù)字,只取N個(gè)數(shù)的元素
skip: 傳入數(shù)字,跳過(guò)N個(gè)元素
last: 取最后一個(gè)元素
first: 取最后一個(gè)元素

<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;

   // 發(fā)出(1, 2, 3, 4, 5)
   const source = from([1, 2, 3, 4, 5]);
   const example = source.pipe(
     // 開(kāi)頭追加 6, 8 得 6, 8, 1, 2, 3, 4, 5
     startWith(6, 8),
     // 舍棄第一個(gè) 得 8, 1, 2, 3, 4, 5
     skip(1),
     // 只取偶數(shù)得 8, 2, 4
     filter(num => num % 2 === 0),
     // 再取前倆得 8, 2
     take(2),
     // 只取最后一個(gè)得 2
     last()
   );
   example.subscribe(val => {
     console.log(`The number: ${val}`)
   });

</script>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容