How to get the last n elements of an Observable?

Asked

Viewed 42 times

2

I own a service that is receiving an array objeto API with the function below:

getUltimosDados(n: number): Observable<Objeto[]>{
    return this.httpClient.get<Objeto[]>(this.enderecoDaApi + '/objetos/');
}

The displayed function returns all objects from within the Observable. But I want to work only with the last n vector objects. How can I do this?

1 answer

3


getUltimosDados(n: number): Observable<Objeto[]>{    
    return this.httpClient.get<Objeto[]>(this.enderecoDaApi + '/objetos/').pipe(
       map(arr=>arr.slice(-n))
    );    
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.