1

I am looping through an existing array and send a request to a REST API to add an additional key/value-pair to my array. My problem now is that the for-loop is faster than my API subscription and I can not properly match the value received from the API. Currently, only the last value is inserted into the array. Thank you for your hints

API Request

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

Add response to my existing array

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...'))

    }
  }
}
Carl Edwards
  • 13,826
  • 11
  • 57
  • 119
mm1975
  • 1,583
  • 4
  • 30
  • 51

0 Answers0