简体   繁体   English

在 for 循环中合并 $http.get JSON 数据

[英]Merge $http.get JSON data in for loop

I'm using a REST server to retrieve data.我正在使用 REST 服务器来检索数据。 In the AngularJS UI the user is given the choice of a few options to create a query to send to the REST server.在 AngularJS UI 中,用户可以选择几个选项来创建要发送到 REST 服务器的查询。 The problem is the server only accepts one of each, so if user wants to search for multiple Entities, they can't.问题是服务器只接受一个,所以如果用户想搜索多个实体,他们不能。 I'm trying to think of a way to send off multiple requests (quantity of requests depends on length of Entity array, which is set by the user in UI).我正在想办法发送多个请求(请求的数量取决于实体数组的长度,它由用户在 UI 中设置)。 So far all I can think of is for loop by the length of the entity array, and for each loop send a request - my problem is how do I join these two sets of data?到目前为止,我能想到的就是通过实体数组的长度进行循环,并为每个循环发送一个请求 - 我的问题是如何加入这两组数据? Each time the for loop completes the data is overridden with the next set it's sent off for.每次 for 循环完成时,数据都会被下一个发送的集合覆盖。 And the amount of times the requests are sent is totally dependent on the amount of entities the user needs returned.并且发送请求的次数完全取决于用户需要返回的实体数量。

If you have any unique identifier for each result item then you can try the following algorithm.如果您对每个结果项都有任何唯一标识符,那么您可以尝试以下算法。 Hopefully It will solve the issue.希望它能解决问题。

var data = []; 
loop through options selected by user {
   request sent {
       on sucess() {
             loop though RESPONSE_DATA_ARRAY {
                 var id = RESPONSE_DATA_ARRAY_ITEM.uniuqe_key
                 if(data[id] === undefined){
                     data[id] = RESPONSE_DATA_ARRAY_ITEM; 
                     //Stored as Key Value pair, which will help to identify same object each time easily.
                 }
             }

       }
   }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM