All you need to know about the count operator in RxJS
The count operator counts the number of values emitted from the source observable.This operator returns an output observable as a number specifying the number of elements emitted by the source observable. The count operator returns the value after the source observable completes.
count(predicate?: Function): OperatorFunctionpredicate: it is a function that returns a boolean and it specifies what values are to be counted.OperatorFunction: A function that returns an observable as a number.
Without the predicate passed the count operator counts all the values emitted by the source observable as given below.
const source = of( 1 , 2 , 3 , 4 , 5 );const result = source.pipe(count());result.subscribe(val => console.log(val))
Since we are passing an observable through the of operator with 5 values, the output of the above program is 5.
With the predicate passed the count operator counts the values emitted by the source observable as per the precondition provided to it.
const numbers = of( 1 , 2 , 3 , 4 , 5 , 6 );const result = numbers.pipe(count(i => i > 3 ));result.subscribe(val => console.log(val));
Here since only 3 values pass the predicate condition, we get 3 as output on the console.
This is pretty much the basics of the count operator.