Categories: RxJS教程

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
terry

这个人很懒,什么都没有留下~

Share
Published by
terry

Recent Posts

聊聊vue3中的defineProps

在Vue 3中,defineP…

3 天 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

1 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

2 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

3 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

3 周 ago

Vue3:手动清理keep-alive组件缓存的方法

Vue3中手动清理keep-a…

4 周 ago