Reputation: 6674
I have a javascript object that maps ids:
codes = {
"admin" : 3,
"manager" : 2,
"basic" : 1
}
Given a value...
accountType = 2;
...what is the most elegant way to find the corresponding key ("manager") ?
Upvotes: 0
Views: 70
Reputation: 700850
If you do this a lot, it would be efficient to keep an object to use for the reverse lookup:
codeNames = {
"3": "admin",
"2": "manager",
"1": "basic"
}
Now you can just access the properties by name:
var name = codeNames[accountType];
You can create the lookup object from the codes
object:
var codeNames = {};
for (var i in codes){
codeNames[codes[i]] = i;
}
Upvotes: 1