gpt4 book ai didi

javascript - RxJS skipWhile 与过滤器

转载 作者:可可西里 更新时间:2023-11-01 02:33:04 28 4
gpt4 key购买 nike

skipWhile 和过滤运算符有什么区别?

const source = interval(1000);
const example = source.pipe(skipWhile(val => val < 5));
const subscribe = example.subscribe(val => console.log(val));


const source = interval(1000);
const example = source.pipe(filter(val => val > 5));
const subscribe = example.subscribe(val => console.log(val));

最佳答案

不同之处在于,在其表达式评估为 false 时,skipWhile 切换到镜像其源可观察对象 - 因此它将停止过滤掉任何进一步的值。

例如:

Observable.from([1,2,3,4,5])
.pipe(filter(val => val % 2 == 0)) // filters out odd numbers
.subscribe(val => console.log(val)); // emits 2,4

Observable.from([1,2,3,4,5])
.pipe(skipWhile(val => val % 2 == 1)) // filters odd numbers until an even number comes along
.subscribe(val => console.log(val)); // emits 2,3,4,5

关于javascript - RxJS skipWhile 与过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49423355/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com