RxJS - Mathematical Operator Max



max() method will take in an observable with all values and return an observable with the max value. It takes in a compare function as an argument, which is optional.

Syntax

max(comparer_func?: number): Observable

Parameters

comparer_func − (optional). A function that will filter the values to be considered for max value from the source observable. If not provided the default function is considered.

Return value

The return value is an observable that will have the max value.

Example 1

The following example is with the max value −

import { of } from 'rxjs';
import { max } from 'rxjs/operators';

let all_nums = of(1, 6, 15, 10, 58, 20, 40);
let final_val = all_nums.pipe(max());
final_val.subscribe(x => console.log("The Max value is "+x));

Output

The Max value is 58

Example 2

The following example is the max value with comparer function −

import { from } from 'rxjs';
import { max } from 'rxjs/operators';

let list1 = [1, 6, 15, 10, 58, 2, 40];
let final_val = from(list1).pipe(max((a,b)=>a-b));
final_val.subscribe(x => console.log("The Max value is "+x));

We are using arrays and the values inside the array are compared using the function given in max function, the max value from the array is returned.

Output

The Max value is 58
Advertisements