I receive a HTTP response, which contains, if all goes well, a single array, coded as JSON.
I want to get this array, filter out some items, and process the passing items as events.
What I do so far is:
return this._http.get(url)
.map((res:Response) => res.json())
.map((data:any) => {
if (!Array.isArray(data) || data.length == 0) {
throw new Error("No items returned, URL: " + url);
}
let projects = <ProjectModel[]>service.fromJSONarray(data, this._http);
return Observable.from(projects)
.filter((project: ProjectModel) => project.parentProject == null)
.subscribe(project -> ...)
})
However I don't like the nesting. I assume there's a way to do this:
return this._http.get(url)
.map((res:Response) => res.json())
.map((data:any) => {
...
let projects = <ProjectModel[]>service.fromJSONarray(data, this._http);
???
})
.filter((project: ProjectModel) => project.parentProject == null)
.subscribe(project -> ...)
How to achieve that?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…