-3

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.

Kawu
  • 13,647
  • 34
  • 123
  • 195

2 Answers2

2

You could use Object.fromEntries to create your map and then access it simply by country code e.g.

const mapOfCountries = Object.fromEntries(
  countries.map(o => [o.isoCode, o])
);
console.log(mapOfCountries['DE'])

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" }
];

const mapOfCountries = Object.fromEntries(
  countries.map(o => [o.isoCode, o])
);

console.log(mapOfCountries['DE']);
console.log(mapOfCountries['CZ']);
Nick
  • 138,499
  • 22
  • 57
  • 95
1

Just use the find(...) method:

const 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"}];

console.log(countries.find(c => c.isoCode === 'DJ'))

Also, for your information - on the link provided, you can see a lot more resources on JavaScript. I recommend you to read through it, its pretty interesting :)

MauriceNino
  • 6,214
  • 1
  • 23
  • 60