我已经为$ 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"
}];
答案 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);