如何在可观察对象中展平可观察对象数组 [英] How to flatten Array of Observables in an Observable
问题描述
我正在尝试展平嵌套的Observable,但我无法使其正常工作:
I'm trying to flatten a nested Observable, but I'm not getting it to work:
this.af.object('test/father')
.map(res => {
res.namedKeys = [];
for (let el in res.keys) {
res.namedKeys.push(this.af.object(`test/keys/${el}`))
}
return res
})
.flatMap(res => Observable.combineLatest(res))
.subscribe(res => {
console.log('The final response:', res);
});
我想获取数组中Observable的实际数据.在过去的两天里,我尝试了很多不同的方法,但是最后,我什么也没收到,或者仍然收到带有Observables的数组.上面的代码未返回任何内容.
I want to get the actual data of the Observables in my Array. I've tried a lot of different things the last 2 days, but in the end, I receive either nothing or still the Array with Observables in it. The code above is returning nothing.
推荐答案
由于您不提供任何结构信息,因此很难看出问题所在.
It's pretty hard to see what is the problem , since you don't supply any structure info.
我认为你的意思是:
for (let el in Object.keys(res))
代替
for (let el in res.keys)
也- combineLatest
希望可以观察到.您没有提供一个:
Also - combineLatest
expects an observable. you didn't supply one :
考虑改用它:
.flatMap(res => Rx.Observable.combineLatest(Rx.Observable.of(res)))
演示(使用rxjs 5使用switchmap而不是flatMap)
http://jsbin.com/cuvisecipi/1/edit?html,js,控制台,输出
Demo ( which uses rxjs 5 which uses switchmap instead of flatMap)
http://jsbin.com/cuvisecipi/1/edit?html,js,console,output
一个更好的演示,它演示了类似的(从数组中)解决方案:
A better demo which illustrates a similar ( from an array) solution :
http://jsbin.com/punoxitino/1/edit?html,js,控制台,输出
这篇关于如何在可观察对象中展平可观察对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!