I have a JavaScript array of objects (essentially originating from JSON data from a web service), where each object represents a country:
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:
How do you best get the entry by isoCode
here?
Maybe I have to turn this into a map (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) beforehand, with the isoCode
being the key and the value being the unchanged object...??
let mapOfCountries = turn_into_map(countries); selectedCountry = mapOfCountries[this.selectedEntity.countryCode]; // or even simpler: // selectedCountry = mapOfCountries['DE'];
How do you best do this in JavaScript / ES6? Maybe a functional approach? I'd rather not like to use a for loop here and check manually. Yuk.