当前位置: 首页 > 文档资料 > 学习 RxJS 操作符 >

every

优质
小牛编辑
119浏览
2023-12-01

every

函数签名: every(predicate: function, thisArg: any): Observable

如果完成时所有的值都能通过断言,那么发出 true,否则发出 false 。

示例

示例 1: 一些值不符合条件

( Stackblitz | jsBin | jsFiddle )

// RxJS v6+
import { every } from 'rxjs/operators';
import { of } from 'rxjs';

// 发出5个值
const source = of(1, 2, 3, 4, 5);
const example = source.pipe(
  // 每个值都是偶数吗?
  every(val => val % 2 === 0)
);
// 输出: false
const subscribe = example.subscribe(val => console.log(val));
示例 2: 所有值都符合条件

( Stackblitz | jsBin | jsFiddle )

// RxJS v6+
import { every } from 'rxjs/operators';
import { of } from 'rxjs';

// 发出5个值
const allEvens = of(2, 4, 6, 8, 10);
const example = allEvens.pipe(
  // 每个值都是偶数吗?
  every(val => val % 2 === 0)
);
// 输出: true
const subscribe = example.subscribe(val => console.log(val));

其他资源

  • every :newspaper: - 官方文档

源码: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/every.ts