欢迎各位兄弟 发布技术文章
这里的技术是共享的
1) import { map } from 'rxjs/operators';
2) 把 map() 改成 pipe(map()) 吧
map(project: Function, thisArg: any): Observable
( StackBlitz | jsBin | jsFiddle )
// RxJS v6+
import { from } from 'rxjs';
import { map } from 'rxjs/operators';
// 发出 (1,2,3,4,5)
const source = from([1, 2, 3, 4, 5]);
// 每个数字加10
const example = source.pipe(map(val => val + 10));
// 输出: 11,12,13,14,15
const subscribe = example.subscribe(val => console.log(val));
( StackBlitz | jsBin | jsFiddle )
// RxJS v6+
import { from } from 'rxjs';
import { map } from 'rxjs/operators';
// 发出 ({name: 'Joe', age: 30}, {name: 'Frank', age: 20},{name: 'Ryan', age: 50})
const source = from([
{ name: 'Joe', age: 30 },
{ name: 'Frank', age: 20 },
{ name: 'Ryan', age: 50 }
]);
// 提取每个 person 的 name 属性
const example = source.pipe(map(({ name }) => name));
// 输出: "Joe","Frank","Ryan"
const subscribe = example.subscribe(val => console.log(val));
map - 官方文档
map vs flatMap - Ben Lesh
转换操作符: map 和 mapTo
- André Staltz
源码: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/map.ts
来自 https://rxjs-cn.github.io/learn-rxjs-operators/operators/transformation/map.html