gpt4 book ai didi

typescript - rxjs 6 - Observable 到 Observable

转载 作者:行者123 更新时间:2023-12-03 23:41:51 28 4
gpt4 key购买 nike

我有一个发出对象数组的可观察对象。我需要使用哪些管道操作符将其转换为 Observable 以便我可以对每个对象进行操作?

我需要对 obs$ 做什么才能让它像 obs2$ 一样发出?

const obs$ = of([{ opponent: 'Walton', team: 'varsity', gametime: new Date() },
{ opponent: 'Scott', team: 'varsity', gametime: new Date() },
{ opponent: 'Dixie', team: 'varsity', gametime: new Date() },
{ opponent: 'Highlands', team: 'freshmen', gametime: new Date() }])
.pipe(
tap(console.log)
);

obs$.subscribe(a =>
console.log(a)
);

const obs2$ = of({ opponent: 'Walton', team: 'varsity', gametime: new Date() },
{ opponent: 'Scott', team: 'varsity', gametime: new Date() },
{ opponent: 'Dixie', team: 'varsity', gametime: new Date() },
{ opponent: 'Highlands', team: 'freshmen', gametime: new Date() })
.pipe(
tap(console.log)
);

obs2$.subscribe(a =>
console.log(a)
);

最佳答案

您需要 mergeAll :

of([2, 3, 4]).pipe(
mergeAll()
).subscribe(v => console.log(v));
// outputs 2, 3, 4

如果您使用 from它也会起作用:

from([2, 3, 4])
.subscribe(v => console.log(v));
// outputs 2, 3, 4

关于typescript - rxjs 6 - Observable<Array(Objects)> 到 Observable<Objects>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50605404/

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