How to find a key by value of a child element?

Tags: , ,



My JSON file (countries_numbers.json):

{
  "AF": {
    "countryName": "Afghanistan",
    "countryPrefix": "93"
  },
  "AL": {
    "countryName": "Albania",
    "countryPrefix": "355"
  },
  "DZ": {
    "countryName": "Algeria",
    "countryPrefix": "213"
  },
  "AS": {
    "countryName": "American Samoa",
    "countryPrefix": "1"
  },
  "AD": {
    "countryName": "Andorra",
    "countryPrefix": "376"
  },
  "AO": {
    "countryName": "Angola",
    "countryPrefix": "244"
  },
  "AI": {
    "countryName": "Anguilla",
    "countryPrefix": "1"
  },
  "AG": {
    "countryName": "Antigua",
    "countryPrefix": "1"
  },
  "AR": {
    "countryName": "Argentina",
    "countryPrefix": "54"
  },
  "AM": {
    "countryName": "Armenia",
    "countryPrefix": "374"
  },
  "AW": {
    "countryName": "Aruba",
    "countryPrefix": "297"
  },
  "AU": {
    "countryName": "Australia",
    "countryPrefix": "61"
  },
  "AT": {
    "countryName": "Austria",
    "countryPrefix": "43"
  },
  "AZ": {
    "countryName": "Azerbaijan",
    "countryPrefix": "994"
  },
  "BH": {
    "countryName": "Bahrain",
    "countryPrefix": "973"
  },
  "BD": {
    "countryName": "Bangladesh",
    "countryPrefix": "880"
  },
  "BB": {
    "countryName": "Barbados",
    "countryPrefix": "1"
  },
  "BY": {
    "countryName": "Belarus",
    "countryPrefix": "375"
  },
  "BE": {
    "countryName": "Belgium",
    "countryPrefix": "32"
  },
  "BZ": {
    "countryName": "Belize",
    "countryPrefix": "501"
  },
  "BJ": {
    "countryName": "Benin",
    "countryPrefix": "229"
  },
  "BM": {
    "countryName": "Bermuda",
    "countryPrefix": "1"
  },
  "BT": {
    "countryName": "Bhutan",
    "countryPrefix": "975"
  },
  "BO": {
    "countryName": "Bolivia",
    "countryPrefix": "591"
  },
  "BA": {
    "countryName": "Bosnia and Herzegovina",
    "countryPrefix": "387"
  },
  "BW": {
    "countryName": "Botswana",
    "countryPrefix": "267"
  },
  "BR": {
    "countryName": "Brazil",
    "countryPrefix": "55"
  },
  "IO": {
    "countryName": "British Indian Ocean Territory",
    "countryPrefix": "246"
  },
  "VG": {
    "countryName": "British Virgin Islands",
    "countryPrefix": "1"
  },
  "BN": {
    "countryName": "Brunei",
    "countryPrefix": "673"
  },
  "BG": {
    "countryName": "Bulgaria",
    "countryPrefix": "359"
  },
  "BF": {
    "countryName": "Burkina Faso",
    "countryPrefix": "226"
  },
  "MM": {
    "countryName": "Burma Myanmar",
    "countryPrefix": "95"
  }
}

Now I want search in this JSON file for a value. Something like that:

SEARCHING: countryPrefix = “226” ¦ WHEN FOUND RETURN: “BF” ELSE RETURN “false”

I hope you new what I want to do. Sry for the bad question, I’m absolute new to JavaScript.

PS: I already searched on Google and found nothing.

Answer

One possible approach:

const countries = {
  "AF": {
    "countryName": "Afghanistan",
    "countryPrefix": "93"
  },
  "AL": {
    "countryName": "Albania",
    "countryPrefix": "355"
  }
  // .. the rest cut for brevity
}

const getCodeByPrefix = prefix => 
  Object.keys(countries).find(code => countries[code].countryPrefix === prefix);

console.log(getCodeByPrefix('93')); // AF
console.log(getCodeByPrefix('193')); // undefined

Here, getCodeByPrefix is a function that takes prefix as argument; its return value is either the code – or undefined, if no value is found for given prefix.


What you’re doing is here is called reverse lookup: trying to find a particular key by its value. The complexity of this operation is O(n), which means the more countries will be in your object, the harder it’ll be to find the one.

Now, as we’re still talking about hundreds of entries, and not thousands (let alone hundreds of thousands), it won’t matter much. Still I’d suggest you to consider reversing the structure of your data to make prefixes keys – either single or duplicating ones.



Source: stackoverflow