首页 > 其他分享 >RxJS 系列 – Mathematical and Aggregate Operators

RxJS 系列 – Mathematical and Aggregate Operators

时间:2023-04-03 22:02:51浏览次数:41  
标签:Mathematical complete max Operators next Aggregate subject

前言

前几篇介绍过了 

Creation Operators

Filtering Operators

Join Creation Operators

Error Handling Operators

Transformation Operators

Join Operators

Utility Operators

Conditional and Boolean Operators

这篇继续介绍 Mathematical and Aggregate Operators

 

参考

Docs – Mathematical and Aggregate Operators

 

count

complete 以后, 统计之前总共发布了几次

const subject = new Subject<number>();
subject.pipe(count()).subscribe({
  next: v => console.log('next', v),
  complete: () => console.log('complete'),
});
subject.complete();

效果

如果一次都没有, 也会 next value 0 哦

 

max & min

complete 后, 找出之前发布过最大/最小的值

const subject = new Subject<{ age: number }>();
subject.pipe(max(person => person.age)).subscribe({
  next: v => console.log('next', v),
  complete: () => console.log('complete'),
});
subject.next({ age: 50 });
subject.next({ age: 72 });
subject.next({ age: 40 });
subject.complete();

效果

 

这里我只给 max 的例子, min 就是找最小.

 

reduce

 

一句话总结

count : complete 后统计之前总共发布几次

max & min : complete 后, 找出之前发布过最大/最小值

reduce :

 

标签:Mathematical,complete,max,Operators,next,Aggregate,subject
From: https://www.cnblogs.com/keatkeat/p/17284593.html

相关文章