组合但删除完整的可观测值

发布于 2025-01-29 12:22:45 字数 323 浏览 2 评论 0原文

如何结合最新值,但可以动态删除完整的可观测值? combineLatest运算符不会删除完整的可观测值,它只是重复其发出的最后一个值,直到所有可观察到所有可观察到。

我想要这样的东西:

combineLatestAlive([interval(100).pipe(take(2)), interval(100)]).subscribe(console.log)

//[0,0]
//[1,0]
//[1,1]
//[1]
//[2] ... and so on

How can I combine the latest values but remove the completed observables dynamically? The combineLatest operator does not remove completed observables, it just repeats the last value they emitted until all observables complete.

I wanted something like this:

combineLatestAlive([interval(100).pipe(take(2)), interval(100)]).subscribe(console.log)

//[0,0]
//[1,0]
//[1,1]
//[1]
//[2] ... and so on

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你是孤单的心事 2025-02-05 12:22:45

我添加了两个操作员。

  1. endWith:像未定义的映射一样的附加端标记
  2. :变换阵列
combineLatest([interval(100).pipe(take(2), endWith(undefined)), interval(100)]).pipe(
    map(values => values.filter(v => v !== undefined))
  ).subscribe(console.log);

结果是:(您想要的结果具有[1,1][1,已经完成值。)

[ 0, 0 ]
[ 1, 0 ]
[ 0 ]
[ 1 ]
[ 2 ]
...

I added two operators.

  1. endWith: Append end mark like undefined
  2. map: Transform array
combineLatest([interval(100).pipe(take(2), endWith(undefined)), interval(100)]).pipe(
    map(values => values.filter(v => v !== undefined))
  ).subscribe(console.log);

result is: (your wanted result has [1, 1] but [1, is already completed value.)

[ 0, 0 ]
[ 1, 0 ]
[ 0 ]
[ 1 ]
[ 2 ]
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文