0

i'm getting started with rxjs in angular2. many of the examples i've seen use simple objects when mapping an http response to an observable.

 this.http.get('/app/shared/data/raw-tasks.json')
        .map(response => response.json())
        .map(stream => stream.map(res => new TaskModel(res.name, res.deadline, res.timeRequired)))

but how do i map a more complicated response like nested objects and nested arrays?

1 Answer 1

6

Once you get the json from the response you can do with it whatever you want. Just add another map function and parse it somewhere else (make sure you return parsed value):

 this.http.get('/app/shared/data/raw-tasks.json')
   .map(response => response.json())
   .map(stream => complicatedParser(stream))

 // somewhere in your code...
 complicatedPatser(stream) {
    return stream
      .map(x => x)
      .filter(x => true)
      .reduce(....)
 }

Observables don't care what they emit, so there's little difference between simple string or complicated nested objects. If you know how to handle basic types, object and/or array response, you know pretty much everything (;

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.