Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an observable that emits subsets of data from another observable

Tags:

rxjs

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?

like image 663
David Barker Avatar asked Nov 29 '25 04:11

David Barker


1 Answers

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.

like image 69
David Barker Avatar answered Dec 02 '25 04:12

David Barker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!