angular2-可观察到的从列表中乱序获取数据 [英] angular2- Observable getting data from list out of order

查看:70
本文介绍了angular2-可观察到的从列表中乱序获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    for (let i = 0; i < this.data.VirtualHosts.length; i++) {
                // get the vh
                this.apiService.findVH(this.data.VirtualHosts[i])
                    // then add each vhost to data by concat and then convert to json
                    .subscribe((data) => {
                        this.data = data.docs[0]
                        this.jsonData = this.jsonData.concat(this.data)
                        //console.log("AFTER: " + this.jsonData[i]._id)
                        //console.log("Response for getVH: " + JSON.stringify(this.jsonData))

                    })
            }

在这里,我遍历了VirtualHosts的ID列表,并对每个ID进行了一次get调用(findVH).但是,get调用似乎在无序调用每个索引.我如何使get调用按this.data.VirtualHosts列表中给出的顺序返回数据列表?

Here, I loop through a list of ID's of VirtualHosts and do a get call (findVH) on every single one of them. However, the get call seems to be calling each index out of order. How would I make it so that the get call returns a list of data in the order that it was given in the this.data.VirtualHosts list?

推荐答案

我认为您需要做两件事.可以使用from运算符将this.data.VirtualHosts转换为可观察的对象,如下所述:

I think you will need to do 2 things. One convert your this.data.VirtualHosts into an observable using the from operator as described here:

https://github .com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/from.md

然后使用concatMap,它将强制api结果按照触发请求的顺序返回.解释如下:

Then use concatMap which will force the api results to return in the order that you are firing off the requests. This is explained here:

https://www.learnrxjs.io/operators/transformation/concatmap.html

这篇关于angular2-可观察到的从列表中乱序获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆