在javascript / typescript数组

时间:2017-12-09 18:19:05

标签: javascript arrays concatenation javascript-objects

我有一个功能,可以通过 odata 从服务器将药物吸引到javascript对象中,它目前在json中返回一系列药物。

如何在连接/连接某些属性的返回对象上创建显示名称属性,以便返回的药物数组具有一个名为displayname的新属性,该属性应等于以下字符串:

 "Dosage:" + drug.Name + "-" + drug.AlternateNames + " (" + drug.Type + ") " + drug.Indication

从服务器提取的代码如下:

getDsTeamSiteDrugs = function () {

    var drugsUrl = environment.odataUrl + "Drug";

    return this.http.get(drugsUrl + '?' + queryStr, options)
    .map((drugs) => this.processDsTeamSiteDrugs(drugs))
}

processDsTeamSiteDrugs(response) {
    return <any[]>response.json()
}

示例响应数组是

[{"deleted":false,"updatedAt":null,"createdAt":"2017-12-09T05:34:30.816Z","version":"AAAAAAAAJ6Q=","id":"48","type":"mg/ml","allConcentration":"15mg/ml","concentration":"15mg/ml","animalTypeId":"af280864-db83-11e7-9296-cec278b6b50a","notes":"q4-6h","perWeightMax":2.00,"perWeightMin":0.25,"perWeight":0.00,"perWeightTypeId":"c51d9efe-db83-11e7-9296-cec278b6b50a","route":"SQ,IM","indication":"Analgesia","alternateNames":"","name":"Morphine","drugCategory":"Analgesic"},{"deleted":false,"updatedAt":null,"createdAt":"2017-12-09T05:34:30.843Z","version":"AAAAAAAAJ7g=","id":"72","type":"mg/ml","allConcentration":"15mg/ml","concentration":"15mg/ml","animalTypeId":"af280e7c-db83-11e7-9296-cec278b6b50a","notes":"q4-6h","perWeightMax":0.30,"perWeightMin":0.10,"perWeight":0.00,"perWeightTypeId":"c51d9efe-db83-11e7-9296-cec278b6b50a","route":"SQ,IM","indication":"Analgesia","alternateNames":"","name":"Morphine","drugCategory":"Analgesic"},{"deleted":false,"updatedAt":null,"createdAt":"2017-12-09T05:34:30.846Z","version":"AAAAAAAAJ70=","id":"78","type":"mg/ml","allConcentration":"15mg/ml","concentration":"15mg/ml","animalTypeId":"af280e7c-db83-11e7-9296-cec278b6b50a","notes":"use with mIdazolam or diazepam","perWeightMax":0.00,"perWeightMin":0.00,"perWeight":0.50,"perWeightTypeId":"c51d9efe-db83-11e7-9296-cec278b6b50a","route":"SQ,IM","indication":"Premed","alternateNames":"","name":"Morphine","drugCategory":"Anesthesia"},{"deleted":false,"updatedAt":null,"createdAt":"2017-12-09T05:34:30.85Z","version":"AAAAAAAAJ74=","id":"79","type":"mg/ml","allConcentration":"15mg/ml","concentration":"15mg/ml","animalTypeId":"af280864-db83-11e7-9296-cec278b6b50a","notes":"given with 0.05mg/kg Ace IM","perWeightMax":0.00,"perWeightMin":0.00,"perWeight":0.50,"perWeightTypeId":"c51d9efe-db83-11e7-9296-cec278b6b50a","route":"SQ,IM","indication":"Premed","alternateNames":"","name":"Morphine","drugCategory":"Anesthesia"}]

预期结果

&#34;用量:&#34; + drug.Name +&#34; - &#34; + drug.AlternateNames +&#34; (&#34; + drug.Type +&#34;)&#34; + drug.Indication 剂量:吗啡 - (mg / ml)麻醉预防

2 个答案:

答案 0 :(得分:0)

使用Promise获取对象数组response.json()后,可以使用then在响应可用后对对象数组进行修改。

processDsTeamSiteDrugs(response) {
  return response.json().then((drugs) => {
    for (const drug of drugs) {
       drug.displayname = `Dosage: ${drug.Name}-${drug.AlternateNames}(${drug.Type}) ${drug.Indication}`;
    }
    return drugs;
  });
}

答案 1 :(得分:0)

感谢指针。我能够使用map运算符和4castles代码来解决这个问题。

 processDsTeamSiteDrugs(response) {

    return response.json().map((drugs) => {
      drugs.displayName = `Dosage: ${drugs.name}-${drugs.alternateNames}(${drugs.type}) ${drugs.indication}`;

      return <any[]>drugs;
    });
  }