简体   繁体   English

使用RXJS映射返回数组/对象而不是可观察对象

[英]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/

RXJS 更好的方法来 map 和过滤器数组内部的 Observable<object><div id="text_translate"><p> 这是我想要完成的一个非常简单的版本。 目前它正在按预期工作,但我只是想知道是否有更短/更清洁的方法来实现 pipe() 内部的运算符</p><p><strong>当前行为:</strong>在包含数组“项目”的属性的可观察对象上调用 pipe。 在 pipe 内部,过滤、排序然后将项目发布到行为主体。</p><pre> public items$: BehaviorSubject public testObservable = () =&gt; of({ Test: '123', Properties: 'props', Items: [ { key: 1, val: 'test1' }, { key: 2, val: 'test2' }, { key: 3, val: 'test3' } ] }); testMethod() { this.testObservable().pipe( pluck('Items'), map(items =&gt; items.filter(item =&gt; item.key,= 2)). map(items =&gt; items,sort((a. b) =&gt; (a.key &gt; b?key: 1, -1))). tap(items =&gt; { this.items$;next(items) }) );</pre></div></object> - RXJS better way to map and filter array inside of Observable<object>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用 rxjs 修改 observable 中的数组,返回整个 object - Modify array in observable with rxjs, return whole object Angular 2和RxJS:使用Rx Observable映射数组 - Angular 2 & RxJS: map an array using Rx Observable 需要RxJS澄清:如何返回普通对象而不是Observable - RxJS clarification needed: how to return a plain object instead of an Observable RXJS映射和过滤器数组-返回单个对象而不是一个对象的数组? - Rxjs map and filter array- Return single object instead of array of one? Angular &amp; Rxjs:如何在返回 Observable 的函数中将 json 映射到对象数组 - Angular & Rxjs: How to map json to object array in a function returning Observable RXJS 更好的方法来 map 和过滤器数组内部的 Observable<object><div id="text_translate"><p> 这是我想要完成的一个非常简单的版本。 目前它正在按预期工作,但我只是想知道是否有更短/更清洁的方法来实现 pipe() 内部的运算符</p><p><strong>当前行为:</strong>在包含数组“项目”的属性的可观察对象上调用 pipe。 在 pipe 内部,过滤、排序然后将项目发布到行为主体。</p><pre> public items$: BehaviorSubject public testObservable = () =&gt; of({ Test: '123', Properties: 'props', Items: [ { key: 1, val: 'test1' }, { key: 2, val: 'test2' }, { key: 3, val: 'test3' } ] }); testMethod() { this.testObservable().pipe( pluck('Items'), map(items =&gt; items.filter(item =&gt; item.key,= 2)). map(items =&gt; items,sort((a. b) =&gt; (a.key &gt; b?key: 1, -1))). tap(items =&gt; { this.items$;next(items) }) );</pre></div></object> - RXJS better way to map and filter array inside of Observable<object> RxJS / Observable flatMap可以返回Observable或数组 - RxJS/Observable flatMap can return Observable or array Rxjs:map 数组字段中的每个元素来自 object 的可观察对象和另一个可观察对象 - Rxjs: map each element in an array field from an observable of object with another observable RxJS 6 过滤并映射可观察的项目数组 - RxJS 6 filter and map an observable array of items RxJs如何在Observable中映射项目<Array> - RxJs how to map the items in an Observable<Array>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM