Scipion
Scipion

Reputation: 11888

Angular2 Deal with Observable<Array<Object>>

I do receive from a service an Observable<Array<Object>>

I would like to apply a function to each element of the Array and return the result in an Observable. I tried the following but it doesn't seem to work

getFoo() {
   let f: Observable<Array<Object>> = serviceCall()
   return f.map(res => res.forEach(v => myFunction(v)))
}

Any idea how I could do this ?

Upvotes: 1

Views: 272

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657078

forEach doesn't return a new array, it just executes the code for each item. Use map() instead:

getFoo() {
   let f: Observable<Array<Object>> = serviceCall()
   return f.map(res => res.map(v => myFunction(v)))
}

Upvotes: 3

Related Questions