Learn RxJS
Search…
map

signature: map(project: Function, thisArg: any): Observable

Apply projection with each value from source.

New to transformation operators? Check out the article Get started transforming streams with map, pluck, and mapTo!

Examples

Example 1: Add 10 to each number
1
// RxJS v6+
2
import { from } from 'rxjs';
3
import { map } from 'rxjs/operators';
4
5
//emit (1,2,3,4,5)
6
const source = from([1, 2, 3, 4, 5]);
7
//add 10 to each value
8
const example = source.pipe(map(val => val + 10));
9
//output: 11,12,13,14,15
10
const subscribe = example.subscribe(val => console.log(val));
Copied!
Example 2: Map to single property
1
// RxJS v6+
2
import { from } from 'rxjs';
3
import { map } from 'rxjs/operators';
4
5
//emit ({name: 'Joe', age: 30}, {name: 'Frank', age: 20},{name: 'Ryan', age: 50})
6
const source = from([
7
{ name: 'Joe', age: 30 },
8
{ name: 'Frank', age: 20 },
9
{ name: 'Ryan', age: 50 }
10
]);
11
//grab each persons name, could also use pluck for this scenario
12
const example = source.pipe(map(({ name }) => name));
13
//output: "Joe","Frank","Ryan"
14
const subscribe = example.subscribe(val => console.log(val));
Copied!

Additional Resources

Last modified 6mo ago