Sudhir Kumar
Sudhir Kumar

Reputation: 153

Can I create a brand new stream from the existing stream in rxjs

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.

Upvotes: 1

Views: 476

Answers (1)

bubbles
bubbles

Reputation: 2717

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

Upvotes: 1

Related Questions