For Example, I have a stream a number of numbers say 1,2,3,4 and so on. I want to sense each of these data and whenever it's even I want to emit true in another data stream. keeping the source data stram[1,2,3,4] as is.
Asked
Active
Viewed 336 times
1
-
Your question lacks of information and not clear at all. – noririco Jul 23 '20 at 12:02
-
Of course you can – martin Jul 23 '20 at 12:07
-
@noririco, For Example, I have a stream a number of numbers say 1,2,3,4 and so on and I want to emit new data whenever it's even keeping the source data stram[1,2,3,4] as it is. – Sudhir Kumar Jul 23 '20 at 12:28
-
I got to know that I can do it with the help of another SubjectObservable. tap on each emitted data from source stream and check the condition and based on that subject will emit new data. – Sudhir Kumar Jul 23 '20 at 12:32
-
You question is a bit unclear, but in my opinion you should read about the differences between cold and hot Observables, you read about it [here](https://stackoverflow.com/questions/2521277/what-are-the-hot-and-cold-observables#:~:text=Hot%20observables%20are%20ones%20that,over%20if%20you%20subscribe%20again.) – Tal Ohana Jul 23 '20 at 12:35
-
I tried to implement like this https://stackblitz.com/edit/angular-rxjs-playground-jnvvle – Sudhir Kumar Jul 23 '20 at 12:43
-
@ohana ive added more details. I hope its clear now. – Sudhir Kumar Jul 23 '20 at 12:55
1 Answers
1
I suggest you to share your source and subscribe to it twice.
...
private source$ = of(1,2,3,4,5,6).pipe(share());
private evenNumberObservable$ = this.source$.pipe(
map(x => x % 2 === 0),
filter(x => !!x)
);
//or
//private evenNumberObservable$ = this.source$.pipe(
// filter(x => x % 2 === 0),
// map(x => true)
//);
public ngOnInit() {
this.evenNumberObservable$.subscribe(x => console.log(x));
this.source$.subscribe(x => console.log(x))
}
...
whole code

bubbles
- 2,597
- 1
- 15
- 40