var StateValue = {
Unknown: 0,
AL: 1,
AK: 2,
AZ: 3,
AR: 4,
CA: 5,
CO: 6,
CT: 7,
DE: 8,
},
Now if i pass 8 i need the value DE to be printed. How can i do this.
var StateValue = {
Unknown: 0,
AL: 1,
AK: 2,
AZ: 3,
AR: 4,
CA: 5,
CO: 6,
CT: 7,
DE: 8,
},
Now if i pass 8 i need the value DE to be printed. How can i do this.
Share Improve this question asked Jun 3, 2011 at 10:40 John CooperJohn Cooper 7,67333 gold badges83 silver badges102 bronze badges 2- 1 You didn't read the link I posted in one of my previous answers, did you? There it is described how to iterate over all properties. At least this is one solution. – Felix Kling Commented Jun 3, 2011 at 10:44
- JavaScript doesn't have enums. I think you're looking to use just a simple array. – Jacob Relkin Commented Jun 3, 2011 at 10:47
1 Answer
Reset to default 9A faster and simpler approach is to use an array:
var StateValues = ['Unknown', 'AL', 'AK', 'AZ', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE'];
alert(StateValues[9]); //'DE'
If for some reason, you need to use your existing structure, try this:
function find_key_by_value(set, value) {
for(var k in set) {
if(set.hasOwnProperty(k)) {
if(set[k] == value) {
return k;
}
}
}
return undefined;
}
alert(find_key_by_value(StateValue, 8));