I'm trying to get a country label (name) from a value (country code) in an object.
In Vue.js I'm trying to build a computed property to return the name of the country based on the country code from an API request.
In the template:
countryLabel () {
var countries = require('../../plugins/countries')
var countryCode = this.content.country
function getLabelByValue(countries, countryCode) {
return Object.keys(countries).find(label => countries[value] === countryCode)
}
}
From a list of countries:
module.exports = [
{ value: 'AF', label: 'Afghanistan' },
{ value: 'AX', label: 'Aland Islands' },
{ value: 'AL', label: 'Albania' },
{ value: 'DZ', label: 'Algeria' },
{ value: 'AS', label: 'American Samoa' },
{ value: 'AD', label: 'Andorra' },
{ value: 'AO', label: 'Angola' },
{ value: 'AI', label: 'Anguilla' },
{ value: 'AQ', label: 'Antarctica' },
{ value: 'AG', label: 'Antigua and Barbuda' },
...
]
0->n. I think what you want is simplycountries.find(country => country.value === countryCode)