RxJS max

2020-10-13 17:43 更新

Max 運(yùn)算符在 Observable 上進(jìn)行操作,該 Observable 發(fā)出數(shù)字(或可以與提供的功能進(jìn)行比較的項(xiàng)目),并且當(dāng)源 Observable 完成時(shí),它會(huì)發(fā)出一個(gè)項(xiàng)目:具有最大值的項(xiàng)目。

max<T>(comparer?: (x: T, y: T) => number): MonoTypeOperatorFunction<T>

參量

comparer 可選的。默認(rèn)值為undefined??蛇x的比較器函數(shù),它將使用它而不是默認(rèn)值來(lái)比較兩個(gè)項(xiàng)目的值。

returns

MonoTypeOperatorFunction<T>:發(fā)射最大項(xiàng)目的 Observable。

描述

最大大理石圖

例子

獲取一系列數(shù)字的最大值

  1. import { of } from 'rxjs';
  2. import { max } from 'rxjs/operators';
  3. of(5, 4, 7, 2, 8).pipe(
  4. max(),
  5. )
  6. .subscribe(x => console.log(x)); // -> 8

使用比較器函數(shù)獲取最大項(xiàng)

  1. import { of } from 'rxjs';
  2. import { max } from 'rxjs/operators';
  3. interface Person {
  4. age: number,
  5. name: string
  6. }
  7. of<Person>(
  8. {age: 7, name: 'Foo'},
  9. {age: 5, name: 'Bar'},
  10. {age: 9, name: 'Beer'},
  11. ).pipe(
  12. max<Person>((a: Person, b: Person) => a.age < b.age ? -1 : 1),
  13. )
  14. .subscribe((x: Person) => console.log(x.name)); // -> 'Beer'

也可以看看

  • min
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)