pluck

signature: pluck(properties: ...args): Observable

Select properties to emit.

pluck - 图1

Examples

Example 1: Pluck object property

( StackBlitz |
jsBin |
jsFiddle )

  1. import { from } from 'rxjs/observable/from';
  2. import { pluck } from 'rxjs/operators';
  3. const source = from([{ name: 'Joe', age: 30 }, { name: 'Sarah', age: 35 }]);
  4. //grab names
  5. const example = source.pipe(pluck('name'));
  6. //output: "Joe", "Sarah"
  7. const subscribe = example.subscribe(val => console.log(val));
Example 2: Pluck nested properties

( StackBlitz |
jsBin |
jsFiddle )

  1. import { from } from 'rxjs/observable/from';
  2. import { pluck } from 'rxjs/operators';
  3. const source = from([
  4. { name: 'Joe', age: 30, job: { title: 'Developer', language: 'JavaScript' } },
  5. //will return undefined when no job is found
  6. { name: 'Sarah', age: 35 }
  7. ]);
  8. //grab title property under job
  9. const example = source.pipe(pluck('job', 'title'));
  10. //output: "Developer" , undefined
  11. const subscribe = example.subscribe(val => console.log(val));

Additional Resources


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