【发布时间】:2019-02-01 15:56:18
【问题描述】:
我正在尝试做一些映射来得到一个对象数组。
this.service.getItems(this.id).pipe(
switchMap(arr => {
const userObservables =
arr.map(collects => this.afs.collection(`users`,
(ref) => ref.where('someId', '==', collects.id)).valueChanges()
.pipe(first())
);
return combineLatest(...userObservables)
.pipe(
map((...eusers) => {
console.log(eusers[0]) //Should be array of objects
arr.forEach((collect, index) => {
collect['username'] = eusers[0][index]['displayName']['username'];
collect['first_name'] = eusers[0][index]['displayName']['first_name'];
collect['last_name'] = eusers[0][index]['displayName']['last_name'];
collect['avatar'] = eusers[0][index]['photoURL'];
});
console.log(arr)
return arr;
})
);
})
)
.subscribe(obj => console.log(obj));
我应该如何重写它以获得我想要的结果(对象数组)?
【问题讨论】:
-
不是 rxjs 方面的专家,但也许可以查看 flatMap 并尝试用 flatMap 替换您的地图功能
-
好的,我也试试.. @OleksandrFedotov
标签: javascript arrays rxjs