-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"
 }];
Sha
  • 1,826
  • 5
  • 29
  • 53

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
  • 18,669
  • 5
  • 37
  • 46