javascript - Use map and switchMap together - Stack Overflow

I have an observable with object containing usual field and other observable. I want to get sequence co

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 values 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 values 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 badges
Add a ment  | 

2 Answers 2

Reset to default 4

Flat 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)
    ))
)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745254175a4618856.html

相关推荐

  • javascript - Use map and switchMap together - Stack Overflow

    I have an observable with object containing usual field and other observable. I want to get sequence co

    2小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信