Search code examples
angularrxjsangular6rxjs6combinelatest

How to subtract 2 observables in RXJS 6


I want to subtract 2 observable numbers

this.num1:Observable<number>
this.num2:Observable<number>
this.num3:Observable<number>

this.num1 = this.store.select(getNum1Count);
this.num2 = this.store.select(getNum2Count);
// this.num3 = difference of this.num1 and this.num2 

in RXJS5 I was doing the below command

 this.num3 = Observable.combineLatest(this.num1,this.num2,(c1,c2)=> Math.abs(c1 - c2));

But in RXJS6, combineLatest is deprecated: Deprecated in favor of static combineLatest as shown here

How do we make this to work in RXJS 6?

On trying the RXJS6 format of combineLatest,

combineLatest(this.num3,this.num2, this.num1, (c1,c2) =>  Math.abs(c1 - c2 )),filter(x => x !== NaN);

I get an error The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.


Solution

  • I was able to get it to work with help ( via gitter @Dorus and @GuillaumeUnice )

    this.num3 = this.num1.pipe(combineLatest(this.num2),map(([n1,n2]) =>  Math.abs(n1 - n2))); 
    

    Observation: As a developer it is weird when just a simple arithmetic operation is this complicated.