Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
139
rated 0 times [  144] [ 5]  / answers: 1 / hits: 58121  / 8 Years ago, sat, september 17, 2016, 12:00:00

In JavaScript code, I have the following enum defined:



MyMessageIds = {
UndefinedId : 0,
FilenameId : 1,
BuildFileId : 2,
MovementArgsId : 3,
MoveId : 4,
ExecuteCommandId : 5
}


In a JavaScript function, I would like to be able to supply the string representation of an enum key (i.e. MoveId) and return its integer value of 4. So how could I do this?


More From » javascript

 Answers
12

Just use bracket notation:



var MyMessageIds = {
UndefinedId : 0,
FilenameId : 1,
BuildFileId : 2,
MovementArgsId : 3,
MoveId : 4,
ExecuteCommandId : 5
};

function getValue(key) {
return MyMessageIds[key];
}

[#60687] Wednesday, September 14, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
amari

Total Points: 736
Total Questions: 111
Total Answers: 90

Location: Saint Pierre and Miquelon
Member since Fri, Jan 28, 2022
2 Years ago
;