Reputation: 139
I have a variable that contains a country name.
var country = 'belgium';
I have a JS data structure:
var countryCoords = {
argentina: '56 100',
belgium: '100 200'
}
How do I get the co-ordinates for Belgium?
Upvotes: 2
Views: 137
Reputation: 13275
You can extract them using country
as the key:
console.log(countryCoords[country]);
Upvotes: 0
Reputation: 22268
Objects support bracket notation and dot notation...
var country = 'belgium';
var countryCoords = {
argentina: '56 100',
belgium: '100 200'
}
countryCoords[country]; // '100 200'
countryCoords.belgium; // '100 200'
Upvotes: 5
Reputation: 382404
you get them with
var coords = countryCoords[country];
Upvotes: 3