Web
Analytics
RxJS operators for Observable emitted values. | filter | timer | interval | From | OF |Repeat | Buffer | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

RxJS operators for Observable emitted values

Filter:

Emit only those observable values which match with the filter condition.

Example: In below example, we use range and filter together, range operator is using to generate sequential values from 0 to100 and filter operator accept range operator output value and filter according to specified condition (values must be completely divisible to 5). So this code will generate values from 0 to 100 which are divisible by 5 only. Subscribe method is used to display observable output on the console.

var ob1 =  Rx.Observable.range(0,100).filter((val) => val%5==0).subscribe(val => console.log(val));




From :

From is used to create observable from another object who generate multiple values like array object.

Example: 

In this example, array1 items are read by observable.

 // Example to create observable from the array using from an operator
  var array1 = ['item1', 'item2', 'item3'];
  var createobusingarray = Rx.Observable.from(array1).subscribe(x=>console.log(x));

Interval:

create an Observable that emits a sequence of integers spaced by a given time interval.

Example :

here I am taking only 10 integer sequence with the help of RxJS take the operator

var intervalex = Rx.Observable.interval(500).take(10).subscribe(x=> console.log(x));




OF:

of operator of RxJS is used only to generate single item only.

Example :

var ofex = Rx.Observable.of(1000).subscribe(x=> console.log(x));

Repeat:

Repeat operator in angular is used to repeat observable output values.

Example:

var repeatinangular = Rx.Observable.range(1,5).repeat(2).subscribe(x=>console.log(x));
   // output - 1,2,3,4,5,1,2,3,4,5,1,2,3,4,5

Timer :

First value after 1/2 second, other sequence value after 1 second.

var bufferex1 = Rx.Observable.timer(500,1000).subscribe(x=> console.log(x));




Buffer :

buffer operator is used to showing values together, a buffer may not be applied on observable, we have to use with other rxjs operators like a timer.

var bufferex = Rx.Observable.timer(0,500).buffer(Rx.Observable.timer(1000,2000)).subscribe(x=> console.log(x));