every

signature: every(predicate: function, thisArg: any): Observable

If all values pass predicate before completion emit true, else false.

every - 图1

Examples

Example 1: Some values false

( jsBin |
jsFiddle )

  1. import { every } from 'rxjs/operators';
  2. import { of } from 'rxjs/observable/of';
  3. //emit 5 values
  4. const source = of(1, 2, 3, 4, 5);
  5. const example = source.pipe(
  6. //is every value even?
  7. every(val => val % 2 === 0)
  8. );
  9. //output: false
  10. const subscribe = example.subscribe(val => console.log(val));
Example 2: All values true

( jsBin |
jsFiddle )

  1. import { every } from 'rxjs/operators';
  2. import { of } from 'rxjs/observable/of';
  3. //emit 5 values
  4. const allEvens = of(2, 4, 6, 8, 10);
  5. const example = allEvens.pipe(
  6. //is every value even?
  7. every(val => val % 2 === 0)
  8. );
  9. //output: true
  10. const subscribe = example.subscribe(val => console.log(val));

Additional Resources


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