I would like to do a backwards selection from the following JSON. I'd like to extract the abbreviation for a particular state. In this situation, the abbreviation is the key, and the value that I'm starting with is the value.
Certainly I can loop through each value, comparing the value to my value, and select the key when the match is made. Is this the best way to approach something like this? Or is there a better way?
Another option would to invert this data early in processing to give myself a similar set of values with the keys/values swapped. I would be interested in seeing methods for doing this efficiently as well.
var States = {AL: 'Alabama', AK: 'Alaska', AZ: 'Arizona', AR: 'Arkansas',
CA: 'California', CO: 'Colorado', CT: 'Connecticut',
DE: 'Delaware', DC: 'District of Columbia', FL: 'Florida',
GA: 'Georgia', HI: 'Hawaii', ID: 'Idaho', IL: 'Illinois',
IN: 'Indiana', IA: 'Iowa', KS: 'Kansas', KY: 'Kentucky',
LA: 'Louisiana', ME: 'Maine', MD: 'Maryland', MA: 'Massachusetts',
MI: 'Michigan', MN: 'Minnesota', MO: 'Missouri', MT: 'Montana',
NE: 'Nebraska', NV: 'Nevada', NH: 'New Hampshire',
NJ: 'New Jersey', NM: 'New Mexico', NY: 'New York',
NC: 'North Carolina', ND: 'North Dakota', OH: 'Ohio',
OK: 'Oklahoma', OR: 'Oregon', PA: 'Pennsylvania',
RI: 'Rhode Island', SC: 'South Carolina',
SD: 'South Dakota', TN: 'Tennessee', TX: 'Texas', UT: 'Utah',
VT: 'Vermont', VA: 'Virginia', WA: 'Washington',
WV: 'West Virginia', WI: 'Wisconsin', WY: 'Wyoming'};
In order to get a key-value pair from a KiiObject, call the get() method of the KiiObject class. Specify the key for the value to get as the argument of the get() method. The value of the key at the first level of the JSON document hierarchy will be obtained.
JSON doesn't have to have only key:value pairs; the specification allows to any value to be passed without a key. However, almost all of the JSON objects that you see will contain key:value pairs.
Keys and values are separated by a colon. Keys must be strings, and values must be a valid JSON data type: string.
In JSON, the “keys” must always be strings. Each of these pairs is conventionally referred to as a “property”. In Python, "objects" are analogous to the dict type. An important difference, however, is that while Python dictionaries may use anything hashable as a key, in JSON all the keys must be strings.
There's no "automatic" way to do this. Your only option is to loop through the list until you find the value that matches the key.
But, if you need to do this multiple times, you should have the code rebuild the JSON object with key/values swapped, so that future lookups are faster. A simple way:
function swapJsonKeyValues(input) {
var one, output = {};
for (one in input) {
if (input.hasOwnProperty(one)) {
output[input[one]] = one;
}
}
return output;
}
var stateAbbrs = swapJsonKeyValues(States);
The only other logical solution would be to have the long name be the key and the abbreviation be the value. Selection is usually made on keys since they are unique, as they should.
You could save yourself from iterating every time you want to get the value, by doing the key - value switch the first time.
function switcharoo(o) {
var t = {};
for (var i in o) {
if(o.hasOwnProperty(i)){
t[o[i]] = i ;
}
}
return t;
}
console.log(switcharoo({AZ: "Arizona"}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With