Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find an element of an array by id with Observable in Angular2

Tags:

I decided to use Observable instead of Http promises.

That is how my Promise service looked:

export class MovieService {      movies: Movie[]     movie: Movie;     constructor(private http:Http) { }     getMovies(): Promise<Movie[]>{          return this.http.get('http://api.request.com')             .toPromise()             .then((res:Response) => res.json()['results'])      }     getMovie(id: number): Promise<Movie> {         return this.getMovies()             .then(movies => movies.find(movie => movie.id == id));      }  } 

First I fetch an array of movies, and than I find a certain movie of the array by id. However when I try to do the same with Observable, I get an error notification on find: Property 'find' does not exist on type 'Movie[]'.

Here is what I tried with the Observable service:

export class MovieService {      movies: Movie[];     movie: Movie;      constructor(private http: Http) {     }      getMovies(): Observable<Movie[]> {          return this.http.get('http://api.request.com)             .map((res: Response) => res.json()['results']);     }      getMovie(id: number): Observable<Movie> {         return this.getMovies()             .subscribe(movies => movies.find(movie => movie.id == id));     } } 

How can I achieve the same functionality in my Observable service just like in my Promise service?

like image 741
GaborH Avatar asked Nov 08 '16 20:11

GaborH


People also ask

How do you access observable arrays?

Reading information from an observableArray So, you can get the underlying JavaScript array by invoking the observableArray as a function with no parameters, just like any other observable. Then you can read information from that underlying array. For example, alert( 'The length of the array is ' + myObservableArray().

How do you know if observable has value?

The RxJS isEmpty() operator returns an observable with a Boolean value indicating whether the observable was empty or not. It returns an output as true if the source observable is empty; otherwise, false.

What is observable angular12?

Angular makes use of observables as an interface to handle a variety of common asynchronous operations. For example: The HTTP module uses observables to handle AJAX requests and responses. The Router and Forms modules use observables to listen for and respond to user-input events.


1 Answers

I suppose you should use map method instead of subscribe which returns Subscription object

export class MovieService {   movies: Movie[];   movie: Movie;    constructor(private http: Http) {}    getMovies(): Observable<Movie[]> {     return this.http.get('http://api.request.com')       .map((res: Response) => res.json()['results']);   }    getMovie(id: number): Observable<Movie> {     return this.getMovies()       .map(movies => movies.find(movie => movie.id == id));   } } 

Plunker Example

like image 64
yurzui Avatar answered Nov 08 '22 14:11

yurzui