Utilizing AngularJS and Lodash, I am making a call to an API that returns JSON data structured as follows:
[{ "_id":"zw",
"name":"Zimbabwe"
},
{ "_id":"au",
"name":"Australia"
},
{ "_id":"in",
"name":"India"
}]
To map country codes to their respective full country names, I have created the following function:
$scope.CountryHashmap = _.reduce(res, function (hash, value) {
var key = value['_id'];
hash[key] = value['name'];
return hash;
}, {});
Upon executing the _.reduce function, the mapped data looks like this:
{
"zw":"Zimbabwe",
"au":"Australia",
"in":"india"
}
On my HTML page, I already have country codes present, and I aim to replace these codes with the corresponding full country names.
HTML:
{{countryCode}}
The countryCodes may include au, in, and zw.