0

I've got an object that looks like

var countryRegions = 
    {
        "config": {
            "show_all_regions": true,
            "regions_required": ["AT", "CA", "EE", "FI", "FR", "DE", "HK", "LV", "LT", "RO", "ES", "CH", "US"]
        },
        "HK": {
            "494": {
                "code": "FN",
                "name": "Hong Kong"
            }
        },
        "CH": {
            "104": {
                "code": "AG",
                "name": "Aargau"
            },
            "106": {
                "code": "AR",
                "name": "Appenzell Ausserrhoden"
            },
            "105": {
                "code": "AI",
                "name": "Appenzell Innerrhoden"
            },
            "108": {
                "code": "BL",
                "name": "Basel-Landschaft"
            },
            "109": {
                "code": "BS",
                "name": "Basel-Stadt"
            },
            "107": {
                "code": "BE",
                "name": "Bern"
            },
            "110": {
                "code": "FR",
                "name": "Freiburg"
            },
            "111": {
                "code": "GE",
                "name": "Genf"
            },
            "112": {
                "code": "GL",
                "name": "Glarus"
            },
            "113": {
                "code": "GR",
                "name": "Graub\u00fcnden"
            },
            "114": {
                "code": "JU",
                "name": "Jura"
            },
            "115": {
                "code": "LU",
                "name": "Luzern"
            },
            "116": {
                "code": "NE",
                "name": "Neuenburg"
            },
            "117": {
                "code": "NW",
                "name": "Nidwalden"
            },
            "118": {
                "code": "OW",
                "name": "Obwalden"
            },
            "120": {
                "code": "SH",
                "name": "Schaffhausen"
            },
            "122": {
                "code": "SZ",
                "name": "Schwyz"
            },
            "121": {
                "code": "SO",
                "name": "Solothurn"
            },
            "119": {
                "code": "SG",
                "name": "St. Gallen"
            },
            "124": {
                "code": "TI",
                "name": "Tessin"
            },
            "123": {
                "code": "TG",
                "name": "Thurgau"
            },
            "125": {
                "code": "UR",
                "name": "Uri"
            },
            "126": {
                "code": "VD",
                "name": "Waadt"
            },
            "127": {
                "code": "VS",
                "name": "Wallis"
            },
            "128": {
                "code": "ZG",
                "name": "Zug"
            },
            "129": {
                "code": "ZH",
                "name": "Z\u00fcrich"
            }
        },
        "US": {
            "1": {
                "code": "AL",
                "name": "Alabama"
            },
            "2": {
                "code": "AK",
                "name": "Alaska"
            },
            "3": {
                "code": "AS",
                "name": "American Samoa"
            },
            "4": {
                "code": "AZ",
                "name": "Arizona"
            },
            "5": {
                "code": "AR",
                "name": "Arkansas"
            },
            "6": {
                "code": "AE",
                "name": "Armed Forces Africa"
            },
            "7": {
                "code": "AA",
                "name": "Armed Forces Americas"
            },
            "8": {
                "code": "AE",
                "name": "Armed Forces Canada"
            },
            "9": {
                "code": "AE",
                "name": "Armed Forces Europe"
            },
            "10": {
                "code": "AE",
                "name": "Armed Forces Middle East"
            },
            "11": {
                "code": "AP",
                "name": "Armed Forces Pacific"
            },
            "12": {
                "code": "CA",
                "name": "California"
            },
            "13": {
                "code": "CO",
                "name": "Colorado"
            },
            "14": {
                "code": "CT",
                "name": "Connecticut"
            },
            "15": {
                "code": "DE",
                "name": "Delaware"
            },
            "16": {
                "code": "DC",
                "name": "District of Columbia"
            },
            "17": {
                "code": "FM",
                "name": "Federated States Of Micronesia"
            },
            "18": {
                "code": "FL",
                "name": "Florida"
            },
            "19": {
                "code": "GA",
                "name": "Georgia"
            },
            "20": {
                "code": "GU",
                "name": "Guam"
            },
            "21": {
                "code": "HI",
                "name": "Hawaii"
            },
            "22": {
                "code": "ID",
                "name": "Idaho"
            },
            "23": {
                "code": "IL",
                "name": "Illinois"
            },
            "24": {
                "code": "IN",
                "name": "Indiana"
            },
            "25": {
                "code": "IA",
                "name": "Iowa"
            },
            "26": {
                "code": "KS",
                "name": "Kansas"
            },
            "27": {
                "code": "KY",
                "name": "Kentucky"
            },
            "28": {
                "code": "LA",
                "name": "Louisiana"
            },
            "29": {
                "code": "ME",
                "name": "Maine"
            },
            "30": {
                "code": "MH",
                "name": "Marshall Islands"
            },
            "31": {
                "code": "MD",
                "name": "Maryland"
            },
            "32": {
                "code": "MA",
                "name": "Massachusetts"
            },
            "33": {
                "code": "MI",
                "name": "Michigan"
            },
            "34": {
                "code": "MN",
                "name": "Minnesota"
            },
            "35": {
                "code": "MS",
                "name": "Mississippi"
            },
            "36": {
                "code": "MO",
                "name": "Missouri"
            },
            "37": {
                "code": "MT",
                "name": "Montana"
            },
            "38": {
                "code": "NE",
                "name": "Nebraska"
            },
            "39": {
                "code": "NV",
                "name": "Nevada"
            },
            "40": {
                "code": "NH",
                "name": "New Hampshire"
            },
            "41": {
                "code": "NJ",
                "name": "New Jersey"
            },
            "42": {
                "code": "NM",
                "name": "New Mexico"
            },
            "43": {
                "code": "NY",
                "name": "New York"
            },
            "44": {
                "code": "NC",
                "name": "North Carolina"
            },
            "45": {
                "code": "ND",
                "name": "North Dakota"
            },
            "46": {
                "code": "MP",
                "name": "Northern Mariana Islands"
            },
            "47": {
                "code": "OH",
                "name": "Ohio"
            },
            "48": {
                "code": "OK",
                "name": "Oklahoma"
            },
            "49": {
                "code": "OR",
                "name": "Oregon"
            },
            "50": {
                "code": "PW",
                "name": "Palau"
            },
            "51": {
                "code": "PA",
                "name": "Pennsylvania"
            },
            "52": {
                "code": "PR",
                "name": "Puerto Rico"
            },
            "53": {
                "code": "RI",
                "name": "Rhode Island"
            },
            "54": {
                "code": "SC",
                "name": "South Carolina"
            },
            "55": {
                "code": "SD",
                "name": "South Dakota"
            },
            "56": {
                "code": "TN",
                "name": "Tennessee"
            },
            "57": {
                "code": "TX",
                "name": "Texas"
            },
            "58": {
                "code": "UT",
                "name": "Utah"
            },
            "59": {
                "code": "VT",
                "name": "Vermont"
            },
            "60": {
                "code": "VI",
                "name": "Virgin Islands"
            },
            "61": {
                "code": "VA",
                "name": "Virginia"
            },
            "62": {
                "code": "WA",
                "name": "Washington"
            },
            "63": {
                "code": "WV",
                "name": "West Virginia"
            },
            "64": {
                "code": "WI",
                "name": "Wisconsin"
            },
            "65": {
                "code": "WY",
                "name": "Wyoming"
            }
        },
        "CA": {
            "66": {
                "code": "AB",
                "name": "Alberta"
            },
            "67": {
                "code": "BC",
                "name": "British Columbia"
            },
            "68": {
                "code": "MB",
                "name": "Manitoba"
            },
            "70": {
                "code": "NB",
                "name": "New Brunswick"
            },
            "69": {
                "code": "NL",
                "name": "Newfoundland and Labrador"
            },
            "72": {
                "code": "NT",
                "name": "Northwest Territories"
            },
            "71": {
                "code": "NS",
                "name": "Nova Scotia"
            },
            "73": {
                "code": "NU",
                "name": "Nunavut"
            },
            "74": {
                "code": "ON",
                "name": "Ontario"
            },
            "75": {
                "code": "PE",
                "name": "Prince Edward Island"
            },
            "76": {
                "code": "QC",
                "name": "Quebec"
            },
            "77": {
                "code": "SK",
                "name": "Saskatchewan"
            },
            "78": {
                "code": "YT",
                "name": "Yukon Territory"
            }
        },
        "AT": {
            "102": {
                "code": "BL",
                "name": "Burgenland"
            },
            "99": {
                "code": "KN",
                "name": "K\u00e4rnten"
            },
            "96": {
                "code": "NO",
                "name": "Nieder\u00f6sterreich"
            },
            "97": {
                "code": "OO",
                "name": "Ober\u00f6sterreich"
            },
            "98": {
                "code": "SB",
                "name": "Salzburg"
            },
            "100": {
                "code": "ST",
                "name": "Steiermark"
            },
            "101": {
                "code": "TI",
                "name": "Tirol"
            },
            "103": {
                "code": "VB",
                "name": "Vorarlberg"
            },
            "95": {
                "code": "WI",
                "name": "Wien"
            }
        }
    }

I've got a select list that looks like:

<select id="billing:region_id">
    <option value="1"> Alabama </option>
    <option value="2"> Arizona </option>
    etc....
</select>

Im trying to loop through the option elements and append their corresponding country code as an id on the element however it is coming back as undefined when I loop like such:

jQuery("#billing\\:region_id option").each(function(k,v){

            v.id = countryRegions['US'][Number(v.value)]['code'];

});

and none of these work

 v.id = countryRegions['US'][Number(v.value)]['code'];
 v.id = countryRegions['US'][Number(v.value)].code;
 v.id = countryRegions.US[Number(v.value)]['code'];

but when I say

 v.id = countryRegions['US'][1]['code'];

this works just fine but each option element gets the first object code as would be expected.

What am I doing wrong here?

7
  • I don't think it should matter, but why are you converting v.value to a number, when the keys in the object are strings? Commented Dec 18, 2015 at 0:49
  • 1
    You have a mistake in the selector. #billing\\:region_id should be #billing\\:region Commented Dec 18, 2015 at 0:54
  • 1
    When I correct that, your code works: jsfiddle.net/barmar/d2dbs39q/1 Commented Dec 18, 2015 at 0:55
  • Sorry that was a typeo the id of the select is billing:region_id ive updated the question. Im making sure the value is a int not string as the key is parsed as an int. Strange though that your code works on fiddle and not on mine. I get TypeError: Cannot read property 'code' of undefined Commented Dec 18, 2015 at 1:16
  • 1
    Maybe you have values in your select that don't have matching elements in the array. Commented Dec 18, 2015 at 1:24

1 Answer 1

1

Add a check that the value exists in the array before trying to use it.

jQuery("#billing\\:region_id option").each(function(k,v){
    if (countryRegion.US[v.value]) {
        v.id = countryRegion.US[v.value].code;
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.