David Barker
David Barker

Reputation: 14620

Create an observable that emits subsets of data from another observable

I have an observable values$ that emits objects.

const value = { a: { foo: bar }};
const values$ = Observable.from([value, value, value]);

I'm trying to create another observable from values$ that emits the value of a everytime values$ fires. I've been scouring the docs looking for an operator that would allow me to do this but I've not found anything.

Right now I am adding extra logic each time the observable is to be subscribed to, but it feels wrong.

const innerValueSubject = new Subject();
const innerValue$ = innerValueSubject.asObservable();

values$.do(value => innerValueSubject.next(value))
    .subscribe(...);

innerValue$.map(value => value.a)
    .subscribe(...);

Is there a way I can derive innerValue$ from values$ and then just map to get the result I need each time values$ emits?

Upvotes: 3

Views: 603

Answers (1)

David Barker
David Barker

Reputation: 14620

Deriving observables from existing observables you just need to reference the existing observable that is returned by each operator.

In my case

const innerValue$ = values$.map(value => value.a);

thanks to @JBNizet for the comment on the question.

Upvotes: 1

Related Questions