本篇文章带大家了解一下Angular RxJS映射数据操作,介绍一下map、mergeMap、switchMap和concatMap函数的使用。
![]()
map
不同之处在于,数组将始终只是数组,而在映射时,将获得数组中当前的索引值。对于 import { of } from "rxjs";
import { map } from "rxjs/operators";
// 创建数据
const data = of([
{
brand: "保时捷",
model: "911"
},
{
brand: "保时捷",
model: "macan"
},
{
brand: "法拉利",
model: "458"
},
{
brand: "兰博基尼",
model: "urus"
}
]);
// 按照brand model的格式输出,结果:["保时捷 911", "保时捷 macan", "法拉利 458", "兰博基尼 urus"]
data.pipe(map(cars => cars.map(car => `${car.brand} ${car.model}`))).subscribe(cars => console.log(cars));
// 过滤数据,只保留brand为porsche的数据,结果:[{"brand":"保时捷","model":"911"},{"brand":"保时捷","model":"macan"}]
data.pipe(map(cars => cars.filter(car => car.brand === "保时捷"))).subscribe(cars => console.log(cars));首先用一系列汽车创建了可观察对象。然后订阅这个可观测值2次。
在这两个例子中,使用 MergeMap现在假设有这样一个场景,有一个可观察到的对象,它发出一个数组,对于数组中的每一项,都需要从服务器获取数据。 可以通过订阅数组来做到这一点,然后设置一个映射来调用一个处理API调用的函数,订阅其结果。如下: import { of, from } from "rxjs";
import { map, delay } from "rxjs/operators";
const getData = param => {
return of(`检索参数: ${param}`).pipe(delay(1000));
};
from([1, 2, 3, 4])
.pipe(map(param => getData(param)))
.subscribe(val => console.log(val));
为了进一步阐明这一点: import { of, from } from "rxjs";
import { map, delay } from "rxjs/operators";
const getData = param => {
return of(`检索参数: ${param}`).pipe(delay(1000));
};
from([1, 2, 3, 4])
.pipe(map(param => getData(param)))
.subscribe(val => val.subscribe(data => console.log(data)));可以想象,这与必须调用 import { of, from } from "rxjs";
import { map, delay, mergeAll } from "rxjs/operators";
const getData = param => {
return of(`检索参数: ${param}`).pipe(delay(1000));
};
from([1, 2, 3, 4])
.pipe(
map(param => getData(param)),
mergeAll()
)
.subscribe(val => console.log(val));这已经好多了, import { of, from } from "rxjs";
import { map, mergeMap, delay, mergeAll } from "rxjs/operators";
const getData = param => {
return of(`检索参数: ${param}`).pipe(delay(1000));
};
// 使用 map
from([1, 2, 3, 4])
.pipe(map(param => getData(param)))
.subscribe(val => val.subscribe(data => console.log(data)));
// 使用 map 和 mergeAll
from([1, 2, 3, 4])
.pipe(
map(param => getData(param)),
mergeAll()
)
.subscribe(val => console.log(val));
// 使用 mergeMap
from([1, 2, 3, 4])
.pipe(mergeMap(param => getData(param)))
.subscribe(val => console.log(val));SwitchMap
import { of, from } from "rxjs";
import { map, delay, switchAll, switchMap } from "rxjs/operators";
const getData = param => {
return of(`retrieved new data with param ${param}`).pipe(delay(1000));
};
// 使用 a regular map
from([1, 2, 3, 4])
.pipe(map(param => getData(param)))
.subscribe(val => val.subscribe(data => console.log(data)));
// 使用 map and switchAll
from([1, 2, 3, 4])
.pipe(
map(param => getData(param)),
switchAll()
)
.subscribe(val => console.log(val));
// 使用 switchMap
from([1, 2, 3, 4])
.pipe(switchMap(param => getData(param)))
.subscribe(val => console.log(val));虽然 import { of, from, BehaviorSubject } from "rxjs";
import { map, delay, switchAll, switchMap } from "rxjs/operators";
const filters = ["brand=porsche", "model=911", "horsepower=389", "color=red"];
const activeFilters = new BehaviorSubject("");
const getData = params => {
return of(`接收参数: ${params}`).pipe(delay(1000));
};
const applyFilters = () => {
filters.forEach((filter, index) => {
let newFilters = activeFilters.value;
if (index === 0) {
newFilters = `?${filter}`;
} else {
newFilters = `${newFilters}&${filter}`;
}
activeFilters.next(newFilters);
});
};
// 使用 switchMap
activeFilters.pipe(switchMap(param => getData(param))).subscribe(val => console.log(val));
applyFilters();正如在控制台中看到的, ConcatMap最后一个例子是 import { of, from } from "rxjs";
import { map, delay, mergeMap, concatMap } from "rxjs/operators";
const getData = param => {
const delayTime = Math.floor(Math.random() * 10000) + 1;
return of(`接收参数: ${param} and delay: ${delayTime}`).pipe(delay(delayTime));
};
// 使用map
from([1, 2, 3, 4])
.pipe(map(param => getData(param)))
.subscribe(val => val.subscribe(data => console.log("map:", data)));
// 使用mergeMap
from([1, 2, 3, 4])
.pipe(mergeMap(param => getData(param)))
.subscribe(val => console.log("mergeMap:", val));
// 使用concatMap
from([1, 2, 3, 4])
.pipe(concatMap(param => getData(param)))
.subscribe(val => console.log("concatMap:", val));
总结将数据映射到所需的格式是一项常见的任务。 概括一下: 更多编程相关知识,请访问:编程视频!! 以上就是浅谈Angular中RxJS如何映射数据操作的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章! |
