javascript - 在 rxjs 中选择重复列表的最后一项
问题描述
我从一个 Socket 中得到一堆流,并想选择最新的项目。
我知道distinctUntilChanged
正在选择第一项,但我需要以某种方式反转此功能。
总之,我想通过 rxjs 做到这一点:
In: 1 - 1 - 1 - 2 - 2 - 3 - 3 - 1 - 1 - 1 - 2 - 2 - 2 - 1 - 2 - 2 - 3 - 3
Out: - - - - 1 - - - 2 - - - 3 - - - - - 1 - - - - - 2 - 1 - - - 2 - 3 - -
解决方案
我们可以在更改后发出最后一个值:
In: 1 - 1 - 1 - 2 - 2 - 3 - 3 - 1 - 1 - 1 - 2 - 2 - 2 - 1 - 2 - 2 - 3 - 3
Out: - - - - - - 1 - - - 2 - - - 3 - - - - - 1 - - - - - 2 - 1 - - - 2 - 3
import { pairwise, take, filter, map, merge, last } from 'rxjs/operators';
import { interval, from } from 'rxjs';
const stream$ = from([1, 1, 1, 2, 2, 3, 3, 1, 1, 1, 2, 2, 2, 1, 2, 2, 3, 3]);
stream$
.pipe(
pairwise(),
filter(([prev, cur]) => prev !== cur),
map(([prev, cur]) => prev),
merge(stream$.pipe(last()))
)
.subscribe(console.log);
推荐阅读
- sql - 使用间接加入的组创建分隔列表
- angularjs - 嵌套单选按钮 angularjs
- java - 带有组件扫描的简单 RestFul WebService
- java - 在 Hybris 中 ant clean all 后,未生成 items.xml 中定义的 itemtype 的相应模型
- github - 强制推送如何影响克隆的分支?
- grails - 登录后如何为用户提供很少的访问控制
- javascript - Sinon.spy 使用导入的方法失败
- vba - Excel vba 动态地从 csv 中选择一列到电子表格中
- eloquent - 如何加入这两个表以获取结果作为 api 资源?
- java - 如何在运行时从 java 程序编译和运行 scala 代码?