Reputation: 99
I have an app in place that returns states abbreviation if the state is clicked. I would like to put a user message in place in the app that would require the actual state name and not the abbreviation.
If I click New York state, I have "NY" stored in a variable named data.name
. How can I use this data to get the full name of a state based off of its abbreviation?
I was thinking of using
var getStateInfo = function (abbrev, stateName) {
}
but I am not sure what I would put inside the function. I am also aware that there's probably a better approach to this. Any help would be great. Thanks!
Upvotes: 4
Views: 9672
Reputation: 43
For those who want to include abbreviations for US territories/not-state jurisdictions as well, here is the complete list:
export const getJurisdictionAbbrev = (jurisdiction: string) =>
({
Arizona: 'AZ',
Armed_Forces_Americas: 'AA',
'Armed_Forces_Europe,_Canada,_Africa_and_Middle_East': 'AE',
Armed_Forces_Pacific: 'AP',
Alabama: 'AL',
Alaska: 'AK',
American_Samoa: 'AS',
Arkansas: 'AR',
California: 'CA',
Colorado: 'CO',
Connecticut: 'CT',
Delaware: 'DE',
District_of_Columbia: 'DC',
Florida: 'FL',
Georgia: 'GA',
Guam: 'GU',
Hawaii: 'HI',
Idaho: 'ID',
Illinois: 'IL',
Indiana: 'IN',
Iowa: 'IA',
Kansas: 'KS',
Kentucky: 'KY',
Louisiana: 'LA',
Maine: 'ME',
Marshall_Islands: 'MH',
Maryland: 'MD',
Massachusetts: 'MA',
Michigan: 'MI',
Micronesia: 'FM',
Minnesota: 'MN',
Mississippi: 'MS',
Missouri: 'MO',
Montana: 'MT',
Nebraska: 'NE',
Nevada: 'NV',
New_Hampshire: 'NH',
New_Jersey: 'NJ',
New_Mexico: 'NM',
New_York: 'NY',
North_Carolina: 'NC',
North_Dakota: 'ND',
Northern_Mariana_Islands: 'MP',
Ohio: 'OH',
Oklahoma: 'OK',
Oregon: 'OR',
Palau: 'PW',
Pennsylvania: 'PA',
Puerto_Rico: 'PR',
Rhode_Island: 'RI',
South_Carolina: 'SC',
South_Dakota: 'SD',
Tennessee: 'TN',
Texas: 'TX',
Utah: 'UT',
Vermont: 'VT',
Virginia: 'VA',
Virgin_Islands: 'VI',
Washington: 'WA',
West_Virginia: 'WV',
Wisconsin: 'WI',
Wyoming: 'WY',
}[jurisdiction]);
Upvotes: 0
Reputation: 2607
I have typescript (could be easy modified to js) function. Convert us state's abbreviation to full name and vice versa
export default function convertUsStateAbbrAndName(input: string): string | null {
const toAbbr = input.length !== 2;
const states = [
['Alabama', 'AL'],
['Alaska', 'AK'],
['American Samoa', 'AS'],
['Arizona', 'AZ'],
['Arkansas', 'AR'],
['Armed Forces Americas', 'AA'],
['Armed Forces Europe', 'AE'],
['Armed Forces Pacific', 'AP'],
['California', 'CA'],
['Colorado', 'CO'],
['Connecticut', 'CT'],
['Delaware', 'DE'],
['District Of Columbia', 'DC'],
['Florida', 'FL'],
['Georgia', 'GA'],
['Guam', 'GU'],
['Hawaii', 'HI'],
['Idaho', 'ID'],
['Illinois', 'IL'],
['Indiana', 'IN'],
['Iowa', 'IA'],
['Kansas', 'KS'],
['Kentucky', 'KY'],
['Louisiana', 'LA'],
['Maine', 'ME'],
['Marshall Islands', 'MH'],
['Maryland', 'MD'],
['Massachusetts', 'MA'],
['Michigan', 'MI'],
['Minnesota', 'MN'],
['Mississippi', 'MS'],
['Missouri', 'MO'],
['Montana', 'MT'],
['Nebraska', 'NE'],
['Nevada', 'NV'],
['New Hampshire', 'NH'],
['New Jersey', 'NJ'],
['New Mexico', 'NM'],
['New York', 'NY'],
['North Carolina', 'NC'],
['North Dakota', 'ND'],
['Northern Mariana Islands', 'NP'],
['Ohio', 'OH'],
['Oklahoma', 'OK'],
['Oregon', 'OR'],
['Pennsylvania', 'PA'],
['Puerto Rico', 'PR'],
['Rhode Island', 'RI'],
['South Carolina', 'SC'],
['South Dakota', 'SD'],
['Tennessee', 'TN'],
['Texas', 'TX'],
['US Virgin Islands', 'VI'],
['Utah', 'UT'],
['Vermont', 'VT'],
['Virginia', 'VA'],
['Washington', 'WA'],
['West Virginia', 'WV'],
['Wisconsin', 'WI'],
['Wyoming', 'WY'],
];
// So happy that Canada and the US have distinct abbreviations
const provinces = [
['Alberta', 'AB'],
['British Columbia', 'BC'],
['Manitoba', 'MB'],
['New Brunswick', 'NB'],
['Newfoundland', 'NF'],
['Northwest Territory', 'NT'],
['Nova Scotia', 'NS'],
['Nunavut', 'NU'],
['Ontario', 'ON'],
['Prince Edward Island', 'PE'],
['Quebec', 'QC'],
['Saskatchewan', 'SK'],
['Yukon', 'YT'],
];
const regions = states.concat(provinces);
let i; // Reusable loop variable
if (toAbbr) {
input = input.replace(/\w\S*/g, function (txt: string) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
for (i = 0; i < regions.length; i++) {
if (regions[i][0] === input) {
return regions[i][1];
}
}
} else {
input = input.toUpperCase();
for (i = 0; i < regions.length; i++) {
if (regions[i][1] === input) {
return regions[i][0];
}
}
}
return null;
}
Inital source - github
Upvotes: 1
Reputation: 2880
You can use a javascript object as a dictionary:
var states = {"NY":"New York", "NJ":"New Jersey"};
and access them like this:
alert(states["NY"]);
so your function that returns the full state name would work like this:
var getStateFullName = function (stateAbbr) {
return states[stateAbbr];
}
Upvotes: 7
Reputation: 3
Could you use a case statement? Have it check data.name, and return the appropriate case? Ex. Switch(data.name) Case 0 (data.name == "NY") return "New York"
Upvotes: 0