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

The groupBy() operator

This operator lets you group the values of an observable sequence. It will generate an observable containing an observable for each group.

It has the following signature:

observable.groupBy(groupFunction,[valueSelector]); 

It receives two parameters, the first mandatory and the second optional:

  • groupFunction: It is the function to decide which group a value belongs to
  • valueSelector: It is a function to select the value to be propagated; if none is specified, it propagates its own value

A simple example usage of this operator would be two odd and even numbers apart in an observable sequence:

Rx.Observable   .of(0,1,2,3,4,5)   .groupBy((i)=>i%2)   .subscribe((obs)=>{  obs.toArray().subscribe((arr)=>console.log(arr)) ...

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