作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一段代码负责获取 Observable<string[]>
,然后将其映射到 Observable<string>
并发出彼此相隔 1 秒的值。
将其想象成网站上的消息自动收报机。
我当前的代码在下面运行:
const arrayOfStrings$ = of([
'Warming things up',
'Getting things ready',
'Welcome'
]);
this.messages$ = arrayOfStrings$.pipe(
switchMap((messages) => from(messages).pipe(
concatMap((innerMessage) => of(innerMessage).pipe(delay(1000))),
)),
tap((message) => {
console.log(`Message: ${message}`);
})
);
有没有更好的方法用更少的代码来做到这一点?主要是switchMap()
和 concatMap()
彼此内心困扰着我。
感谢您的帮助和建议。
编辑:这是我在实际项目中运行的简化版本。我只是想找到一种更简单的方法来从 Observable
最佳答案
您可以先展平数组,然后使用 concatMap()
延迟每次发射,这将确保一条一条地发射消息。
const arrayOfStrings$ = of([
'Warming things up',
'Getting things ready',
'Welcome'
]);
arrayOfStrings$
.pipe(
concatAll(), // flatten the array into individual next notifications
concatMap(message => of(message).pipe(
delay(1000),
)),
)
.subscribe();
关于javascript - RxJS 发出字符串数组,彼此相隔一秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68214987/
我尝试获取各种 pandas 日期的日期增量。但是,对于 >292 年的时间增量,我得到负值。例如, import pandas as pd dates = pd.Series(pd.date_ran
我是一名优秀的程序员,十分优秀!