考虑以下流:
--'[a,b,c]'--
'[a,b,c]'
是流中的一个项目。现在我正在寻找一种方法将该流映射到这个:
--'a'--'b'--'c'--
我认为在某些时候我需要使用map
,因为我只知道如何拆分数组:
Observable.from(['[a,b,c]'])
.map(i => {
let arr = JSON.parse(i);
//Somehow inject the arr items into the stream (instead of arr itself)
})
.subscribe(console.log);
我希望在控制台中看到三个单独的条目a
,b
和c
。
答案 0 :(得分:3)
只需使用mergeMap
代替map
:
Rx.Observable
.from(['["a", "b", "c"]'])
.mergeMap(value => JSON.parse(value))
.subscribe(value => console.log(value));

.as-console-wrapper { max-height: 100% !important; top: 0; }

<script src="https://unpkg.com/rxjs@5/bundles/Rx.min.js"></script>
&#13;
mergeMap
将展平项目函数返回的数组。