RXJS: SwitchMap converts string into single characters unexpectedly
I think you need map
instead of switchMap
.
The function passed to switchMap
expects to return an observable or promise. "a" + str
is not.
I guess you meant to use map
instead of switchMap
.
switchMap
actually expects an ObservableInput
as return-value, which in your case is a string, which in turn is treated as an array and therefore split up into single elements.
To have your expected result with switchMap
you could do:
o.switchMap(str => Observable.of("a" + str))
.subscribe(str => console.log(str));
But better use:
o.map(str => "a" + str)
.subscribe(str => console.log(str))
New syntax with pipe:
o.pipe(
map(str => "a" + str)
).subscribe(str => console.log(str))