简体   繁体   中英

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM