I've got a JavaScript array filled with objects, which in this case represent different countries pulled from JSON data via a web service:
let countries = [
...,
{
"id": 46,
"type": "COUNTRY",
"name": "Cape Verde",
"isoCode": "CV",
"isoNbr": "132"
},
{
"id": 52,
"type": "COUNTRY",
"name": "Christmas Island",
"isoCode": "CX",
"isoNbr": "162"
},
{
"id": 63,
"type": "COUNTRY",
"name": "Cyprus",
"isoCode": "CY",
"isoNbr": "196"
},
{
"id": 64,
"type": "COUNTRY",
"name": "Czech Republic",
"isoCode": "CZ",
"isoNbr": "203"
},
{
"id": 88,
"type": "COUNTRY",
"name": "Germany",
"isoCode": "DE",
"isoNbr": "276"
},
{
"id": 66,
"type": "COUNTRY",
"name": "Djibouti",
"isoCode": "DJ",
"isoNbr": "262"
},
...
];
QUESTION:
What's the most efficient way to retrieve an entry based on its isoCode
value within this array?
Would converting this into a map (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) beforehand, using isoCode
as the key and the object itself as the value, be a good approach...??
let mapOfCountries = turn_into_map(countries);
selectedCountry = mapOfCountries[this.selectedEntity.countryCode];
// or even simpler:
// selectedCountry = mapOfCountries['DE'];
What is the most effective way to achieve this in JavaScript / ES6? Any thoughts on applying a functional methodology here instead of resorting to a manual loop check? Not really keen on the traditional for loop method. Yuk.