在for循环中订阅可观察项

时间:2018-08-14 13:00:00

标签: javascript typescript

我正在遍历现有数组,并向REST API发送请求,以向我的数组添加其他键/值对。我现在的问题是for循环比我的API订阅快,而且我无法正确匹配从API接收的值。当前,仅最后一个值插入到数组中。谢谢您的提示

API请求

getSynonyms(icd): Observable<any> {
  return this.http.get(this.synonymsURL + '?filter[meta_key]=code&filter[meta_value]=' + icd);
}

向我现有的阵列添加响应

retrieveSynonyms() {

var obj = this.cardArray;

for (var item in obj) {

  var str = obj[item].type;
  if (str === 'test') {
        var icd = obj[item].conceptId;
        this.restProvider.getSynonyms(icd)
          .subscribe(response => {
            // wait for response from subscribe
            if (response != null) {
              var data = response[0].synonym_type;
              var synonyms = data.replace(/[|]/g,", ");
              obj[item]["otherSynonyms"]=synonyms;
            }
          }, error => console.log('Retrieve Synonyms: Uppsss something went wrong...'))

    }
  }
}

0 个答案:

没有答案