O'Reilly logo

Mastering Reactive JavaScript by Erich de Souza Oliveira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Aggregating data

The most important operator to aggregate data is the reduce() operator, as the other operators the reduce() operator returns a new observable; in this case the observable returned contains a single value that is the result of the execution of a function over all the elements of the original observable.

We can use the reduce() operator to find the lower numeric value in an observable:

Rx.Observable   .of(1,5,0,9,56,-2,13)   .reduce(function(a,b){     if(a<b){       return a;     }else{       return b;     }   })   .subscribe((i)=>console.log(i)); 

In this example, the values from the observable are going to be scanned from left to right, and the reduce() function is used to check which one is lower (the if/else block), and then we log the result. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required