【问题标题】:Data massaging in json response [duplicate]json响应中的数据按摩[重复]
【发布时间】:2016-11-10 15:48:04
【问题描述】:

我已经为 $http get 服务调用编写了以下代码

  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"
 }];

【问题讨论】:

  • 你想给你的 JSON 响应什么样的按摩?

标签: javascript angularjs json object


【解决方案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);

【讨论】:

    猜你喜欢
    • 2020-08-06
    • 2019-05-16
    • 2023-03-16
    • 2015-11-18
    • 2022-01-07
    • 2018-12-21
    • 2019-01-23
    • 1970-01-01
    • 2019-12-20
    相关资源
    最近更新 更多