admin管理员组文章数量:1416642
I have an observable with object containing usual field and other observable. I want to get sequence containing this field and the value of corresponding observable firing together with inner observable.
For example:
var smth$ = interval(1000).pipe(
take(3),
map(i => ({
id: String.fromCharCode('A'.charCodeAt(0) + i),
value$: interval(300).pipe(
take(10),
map(j => i*10 + j)
)
}))
)
I can easily get sequence of id
field via map
:
smth$.pipe(
map(x => x.id)
)
Also I can get sequence of value
s via switchMap
:
smth$.pipe(
switchMap(x => x.value$)
)
But how can I get a sequence of pairs with both id
and value
?
Runable example:
I have an observable with object containing usual field and other observable. I want to get sequence containing this field and the value of corresponding observable firing together with inner observable.
For example:
var smth$ = interval(1000).pipe(
take(3),
map(i => ({
id: String.fromCharCode('A'.charCodeAt(0) + i),
value$: interval(300).pipe(
take(10),
map(j => i*10 + j)
)
}))
)
I can easily get sequence of id
field via map
:
smth$.pipe(
map(x => x.id)
)
Also I can get sequence of value
s via switchMap
:
smth$.pipe(
switchMap(x => x.value$)
)
But how can I get a sequence of pairs with both id
and value
?
Runable example: https://rxviz./v/R85xKw6J
Share Improve this question edited Sep 21, 2018 at 19:40 CozyAzure 8,4787 gold badges38 silver badges55 bronze badges asked Sep 21, 2018 at 18:47 QwertiyQwertiy 21.6k17 gold badges67 silver badges142 bronze badges2 Answers
Reset to default 4Flat solution: https://rxviz./v/j8ArKWEo
smth$.pipe(
switchMap(x => x.value$, (x, d) => x.id + d)
)
You can try
smth$.pipe(
switchMap(x => x.value$.pipe(
map(d => x.id + d)
))
)
本文标签: javascriptUse map and switchMap togetherStack Overflow
版权声明:本文标题:javascript - Use map and switchMap together - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745254175a2650001.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论