3

Consider the following stream:

--'[a,b,c]'--

The '[a,b,c]' is one item in the stream. Now I'm looking for a way to map that stream into this:

--'a'--'b'--'c'--

I think at some point I need to use map since I only know how to split the array:

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

I wish to see three separate entries in the console with a, b and c.

Mehran
  • 15,593
  • 27
  • 122
  • 221

1 Answers1

3

Just use mergeMap instead of 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>

mergeMap will flatten an array returned by the project function.

cartant
  • 57,105
  • 17
  • 163
  • 197