every

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

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

every - 图1

示例

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

( jsBin |
jsFiddle )

  1. import { every } from 'rxjs/operators';
  2. import { of } from 'rxjs/observable/of';
  3. // 发出5个值
  4. const source = of(1, 2, 3, 4, 5);
  5. const example = source.pipe(
  6. // 每个值都是偶数吗?
  7. every(val => val % 2 === 0)
  8. );
  9. // 输出: false
  10. const subscribe = example.subscribe(val => console.log(val));
示例 2: 所有值都符合条件

( jsBin |
jsFiddle )

  1. import { every } from 'rxjs/operators';
  2. import { of } from 'rxjs/observable/of';
  3. // 发出5个值
  4. const allEvens = of(2, 4, 6, 8, 10);
  5. const example = allEvens.pipe(
  6. // 每个值都是偶数吗?
  7. every(val => val % 2 === 0)
  8. );
  9. // 输出: true
  10. const subscribe = example.subscribe(val => console.log(val));

其他资源


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