RxJS - 數學運算子 Max



max() 方法將接受包含所有值的可觀察物件並返回包含最大值的可觀察物件。它把比較函式作為引數,此函式是可選的。

語法

max(comparer_func?: number): Observable

引數

comparer_func − (可選)。一個函式,它將篩選出要從源可觀察物件中的最大值考量的值。如果不提供,則將考慮預設函式。

返回值

返回值是包含最大值的可觀察物件。

示例 1

以下示例具有最大值 −

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));

輸出

The Max value is 58

示例 2

以下示例為具有比較函式的最大值 −

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));

我們使用陣列並且陣列中的值使用 max 函式中給出的函式進行比較,陣列中的最大值將被返回。

輸出

The Max value is 58
廣告
© . All rights reserved.