How to extract data from RxJS observable

Jody

I've been learning Angular recently and am trying to better understand RxJS. Assume I have an API response from HttpClient.get that gives me an Observable of the following JS object:

const response = {
  photos: [
    {
      id: 1,
      src: '1.jpg',
    },
    {
      id: 2,
      src: '2.jpg',
    },
    {
      id: 3,
      src: '3.jpg',
    }
  ]
}

The end result I'm looking for is: ['1.jpg', '2.jpg', '3.jpg']

I am able to do the following to get each individual src one at a time:

const source = from(response.photos);
const images = source.pipe(
    pluck('src')
);
images.subscribe({
    next(x) { 
        console.log(x); // results in: 1.jpg 2.jpg 3.jpg, one item per line.
    }
});

But what I'd like to do, and what I thought would work, is to create an observable of the entire response (as it would be given from Angular's http.get() call) and return all src properties as an array, like this:

const source = of(response);
const images = source.pipe(
    pluck('photos'),  // i expect this to be [{ id: 1, src: '1.jpg' }, ... ]
    map(x => x.src),  // i expect x to to be { id: 1, src: '1.jpg' } (but it is not)
);
images.subscribe({
    next(x) { 
        console.log(x); // i expect x to be the result of the map call earlier, i.e. ['1.jpg', '2.jpg', '3.jpg']
    }
});

I suppose I'm likely getting hung up on expecting the final value vs. the values as they arrive, or something to that effect. If anyone can help explain how to get the desired result or suggest a different/better approach, I'm hoping that something will click and things will start to make more sense.

Yasser Nascimento

As you need just some data manipulation after get the source's result without any event manipulation, I like to simply use a map like this:

const source = of(response);
const images = source.pipe(map(data => data.photos.map(x => x.src)));
images.subscribe(console.log);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to extract data from an Observable

How to extract data from Observable?

How to extract data from an Observable in Angular

RxJs- Observable - How to fetch data from JSON Object

How to store observable data from API with rxJS in Angular 5?

how to change the data of the first observable using data from another observable including the condition in angular/rxjs

Retrieve data from RxJS observable in HttpModule

Using RXJS to extract single value from observable with no mutation

How to update data in Observable in Angular? rxjs

Transform data from one observable using another observable in angular rxjs

Angular RxJS add data from new observable to current observable

How to unsubscribe from an RxJS 5 observable?

How to get number of ticks from rxjs Observable

How to create an RxJs Observable from a node request

Extract data from observable in Angular 2

Angular how do I filter the data from an observable using rxjs pipe

How to perform filtering for an observable loading data from a server using a form control using rxJS?

RxJs Observable in Observable from Array

Difference in importing Observable from 'rxjs/Observable' and 'rxjs'

RxJava: How to extract object from observable?

Angular 5 / Typescript/RxJS - get data from Observable

Best way to get intermediate data from an RXJS observable

Using MergeMap with the array of data received from another observable - RxJs Angular

Access my returned data from an RxJs Observable method call

rxjs: how to return the result from another observable from catchError

How to trigger an rxjs observable

How to chain rxjs observable

How to return observable with RxJS in proper format with additional separate paging data

How to supply an Rxjs observable as data for a method using combineLatest in Jasmine