[英]Return an array/object instead of an observable using RXJS map
I have the following code which returns an observable with the item details for each item key: 我有以下代码,它返回一个带有每个项键的项详细信息的observable:
this.list = this.af.database.list("API_URL")
.map((ItemKeys) => {
return ItemKeys.map((ItemKey) => {
return this.af.database.object(API_URL + "/" + ItemKey); //I need this as the object (not observable)
});
}).subscribe((items) => {
this.lineData = items; //A list of observables
});
The code itereates over each item key and gets the item data from another endpoint. 代码迭代每个项密钥并从另一个端点获取项数据。 The problem is that it retruns an observable of each item data, and I need it in an object (the observable result).
问题是它重新编译每个项目数据的可观察量,我需要它在一个对象(可观察的结果)中。
I tried to return the subscription result with the following code, but then all my items data are empty (undefined): 我尝试使用以下代码返回订阅结果,但随后我的所有项目数据都为空(未定义):
this.list = this.af.database.list("API_URL")
.map((ItemKeys) => {
return orderItemKeys.map((ItemKey) => {
this.af.database.object("API_URL" + "/ItemKey.$key) // So I tried to subscribe to this..
.subscribe((data) => {
return data;
});
});
}).subscribe((items) => {
this.lineData = items; // A list with undefined items
});
How I can return the item data object as an object and not as observable of an object in my case? 如何将项数据对象作为对象返回,而不是在我的情况下作为对象的可观察对象?
Don't return a subscription (from subscribe) inside an operator. 不要在运营商内返回订阅(来自订阅)。 That's probably not want you intend to do.
那可能不是你想打算做的。
For the inner part, where you retrieve a list of objects, you should be able to turn an array of observables created from Array#map()
into a higher-order observable with Observable#from()
. 对于检索对象列表的内部部分,您应该能够将从
Array#map()
创建的可观察Array#map()
转换为具有Observable#from()
的高阶observable。 You need mergeAll()
to take this series of observables back to a single stream, then collect all emitted items into an array with toArray()
. 您需要
mergeAll()
将这一系列的observable恢复为单个流,然后使用toArray()
将所有发出的项收集到一个数组中。 Then you extract the last observable with mergeMap()
. 然后使用
mergeMap()
提取最后一个observable。
this.list = this.af.database.list("API_URL")
.mergeMap((ItemKeys) => {
return Observable.from(ItemKeys.map((ItemKey) => {
return this.af.database.object(API_URL + "/" + ItemKey);
})).mergeAll().toArray();
})
.subscribe((items) => {
this.lineData = items;
});
You should replace map
with the flatMap
, as it is capable of flattening Observable<Observable<T>>
. 你应该用
flatMap
替换map
,因为它能够展平Observable<Observable<T>>
。
var responseStream = requestStream
.flatMap(function(requestUrl) {
return Rx.Observable.fromPromise(jQuery.getJSON(requestUrl));
});
responseStream.subscribe(() => {});
Trying to adapt and simplify your code to this 尝试调整和简化您的代码
const databaseList = () => Rx.Observable.from(['a6b54x4', '1e23sa4q']);
const databaseObject = (id) => Rx.Observable.of({ id, status: 'ok' })
databaseList()
.flatMap(x => databaseObject(`id/${x}`))
.reduce((x, y) => x.concat(y), [])
.subscribe(console.log);
Basically, it still flattens Observable<Observable<T>>
and then using reduction, merges everything into a result array. 基本上,它仍然使
Observable<Observable<T>>
变平,然后使用reduction,将所有内容合并到一个结果数组中。
https://jsfiddle.net/6pLpo3cp/ https://jsfiddle.net/6pLpo3cp/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.