On this page
map
function
stable
operator
Applies a given project
function to each value emitted by the source Observable, and emits the resulting values as an Observable.
map<T, R>(project: (value: T, index: number) => R, thisArg?: any): OperatorFunction<T, R>
Parameters
project |
(value: T, index: number) => R |
The function to apply to each |
thisArg |
any |
Optional. Default is An optional argument to define what |
Returns
OperatorFunction<T, R>
: A function that returns an Observable that emits the values from the source Observable transformed by the given project
function.
Description
Like Array.prototype.map(), it passes each source value through a transformation function to get corresponding output values.
Similar to the well known Array.prototype.map
function, this operator applies a projection to each value and emits that projection in the output Observable.
Example
Map every click to the clientX
position of that click
import { fromEvent, map } from 'rxjs';
const clicks = fromEvent<PointerEvent>(document, 'click');
const positions = clicks.pipe(map(ev => ev.clientX));
positions.subscribe(x => console.log(x));
See Also
© 2015–2022 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/index/function/map