Home » RxJS Count() Mathematical Operator

RxJS Count() Mathematical Operator

by Online Tutorials Library

RxJS Count() Mathematical Operator

The RxJS count() operator is a mathematical operator that takes in an Observable with values and converts it into an Observable that will give a single counted value. It also acts as a function that takes in predicate function as an optional argument. This is a Boolean type function and will add value to the output only if the value is true.

Syntax:

Following is the syntax of count() operator:

Parameter Explanation

predicate_func: It is used to filter the values that are counted from the source observable and return a boolean value. It is optional.

Return value

The count() operator’s return value is observable that provides the count of the given numbers.

Let us see some examples of count() operator to understand it clearly.

Example 1

Example of the count() operator without predicate and with function. See the following example:

Output:

After executing the above example, you will see the following result:

RxJS Count() Mathematical Operator

In the above example, you can see that the counted values are 8 which is correct.

Example 2

See the example of the count() operator with a predicate function:

Output:

After executing the above example, you will see the following result:

RxJS Count() Mathematical Operator

Here, you can see that it has given the count only of even numbers.


Next TopicRxJS Operators

You may also like