title: Transform data in an Observable
body: <p>I'm retrieving a list of items from an Observable on my Angular service. Inside every item, I have an array with a list of subjects. For every subject, I need to make another call to get its details (e.g. name, description, etc.).</p>
  
  <p><strong>Data structure:</strong></p>
  
  <pre><code>- post1
    - subjects: ['books', 'cars', 'movies']
  </code></pre>
  
  <p>With that list, I have the <code>id</code> from every subject but I need to return an observable containing all subjects belonging to that post (and their details).</p>
  
  <p><strong>AppService.ts</strong></p>
  
  <pre><code>getPost(id: string): Observable&lt;Post&gt; {
      return Observable.of({id: id, subjects: ['books', 'cars', 'movies']});
  }
  
  getSubjects(id: string): Observable&lt;Subject[]&gt; {
      return this.getPost(id).pipe(
          map((post: Post) =&gt; post.subjects),
          mergeMap((subjects: string[]) =&gt; subjects.map((id: string) =&gt; this.getSubject(id))),
          switchMap(list =&gt; list),
      );
  }
  
  getSubject(id: string): Observable&lt;Subject&gt; {
      switch (id) {
          case 'books':
              return Observable.of({id: 'books', name: 'Books'});
          case 'cars':
              return Observable.of({id: 'cars', name: 'Cars'});
          case 'movies':
              return Observable.of({id: 'movies', name: 'Movies'});
      }
  }
  </code></pre>
  
  <p>It returns a stream of objects but I'd like to return an array with all subjects.</p>
  
  <p><strong>[<a href="https://stackblitz.com/edit/angular-mgdvqw?file=app%2Fapp.service.ts" rel="nofollow noreferrer">DEMO</a>]</strong></p>