json响应中的数据按摩

时间:2016-11-10 15:48:04

标签: javascript angularjs json object

我已经为$ http get service service

编写了以下代码
  var country = "http://localhost:3000/json/country.json";
        $http.get(timeZoneUrl).then(function (response) {
            $ctrl.timezone = response.data;
            console.log(response.data);
        });

一切正常,我得到的反应如下。

  [{
    "region": "Africa",
    "country": "South Africa"
  },
  {
    "region": "Europe",
    "country": "Spain"
  }];

我想按摩json响应并从UI更新密钥。我希望响应采用以下格式

 [{
     "value": "Africa",
     "label": "South Africa"
   },
   {
     "value": "Europe",
     "label": "Spain"
 }];

1 个答案:

答案 0 :(得分:1)

您可以使用Array.prototype.map();

var countries = [{"region": "Africa","country": "South Africa"},{"region": "Europe","country": "Spain"}],
    formattedCountries = countries.map(function(c) {
      return {
        'value': c.region,
        'label': c.country
      };
    });

console.log(formattedCountries);