-2

I have written the following code for in angular for a $http get service call

  var country = "http://localhost:3000/json/country.json";
        $http.get(timeZoneUrl).then(function (response) {
            $ctrl.timezone = response.data;
            console.log(response.data);
        });

Everything works fine and i am getting response as below.

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

I would like to have the json response massaged and to update the key from the UI. i want the response to be in the following format

 [{
     "value": "Africa",
     "label": "South Africa"
   },
   {
     "value": "Europe",
     "label": "Spain"
 }];
Shareer
  • 1,546
  • 4
  • 22
  • 45

1 Answers1

1

You can use 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);
Yosvel Quintero Arguelles
  • 17,270
  • 4
  • 37
  • 42