12

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?

3
  • Maybe I don't understand your question completely, but you just use MyMessageIds.MoveId (or alternatively MyMessageIds["MoveId"] to get the integer value.
    – jeffjenx
    CommentedSep 19, 2016 at 17:33
  • MyMessageIds["MoveId"] --- that's what I was looking for ... thanks.
    – chuckp
    CommentedSep 19, 2016 at 18:03
  • 1
    How do i go the opposite way? I have the number and want the name?CommentedMar 7, 2018 at 19:20

2 Answers 2

26

Just use bracket notation:

var MyMessageIds = { UndefinedId : 0, FilenameId : 1, BuildFileId : 2, MovementArgsId : 3, MoveId : 4, ExecuteCommandId : 5 }; function getValue(key) { return MyMessageIds[key]; } 
1
  • 1
    Additionally, you should protect the "de facto" Enum from changes by applying Object.freeze() to it.
    – Stacky
    CommentedJul 2, 2018 at 10:15
5

You could create some utility methods which take an object (enum) that figures out how to get the keys/values.

var MyMessageIds = { UndefinedId : 0, FilenameId : 1, BuildFileId : 2, MovementArgsId : 3, MoveId : 4, ExecuteCommandId : 5 } function getEnumKeys(enumType) { return Object.keys(MyMessageIds); } function getEnumValues(enumType) { return getEnumKeys(enumType).map(function(key) { return enumType[key]; }); } function getEnumValue(enumType, key) { return enumType[getEnumKeys(enumType).filter(function(k) { return key === k; }).pop() || '']; } document.body.innerHTML = '<pre>' + JSON.stringify({ 'Enum Keys' : getEnumKeys(MyMessageIds), 'Enum Vals' : getEnumValues(MyMessageIds), 'Example' : { 'MoveId' : getEnumValue(MyMessageIds, 'MoveId') } }, null, 4) + '</pre>';

You could also create your own class object to represent an enum which has reusable methods.

function Enum() { this.self = arguments[0]; } Enum.prototype = { keys : function() { return Object.keys(this.self); }, values : function() { var me = this; return this.keys(this.self).map(function(key) { return me.self[key]; }); }, getValueByName : function(key) { return this.self[this.keys(this.self).filter(function(k) { return key === k; }).pop() || '']; }, getNameByValue : function(value) { var me = this; return this.keys(this.self).filter(function(k) { return me.self[k] === value; }).pop() || null; } }; var MyMessageIds = new Enum({ UndefinedId : 0, FilenameId : 1, BuildFileId : 2, MovementArgsId : 3, MoveId : 4, ExecuteCommandId : 5 }); document.body.innerHTML = '<pre>' + JSON.stringify({ 'Enum Keys' : MyMessageIds.keys(), 'Enum Vals' : MyMessageIds.values(), 'Example' : { 'MoveId' : MyMessageIds.getValueByName('MoveId'), 'Val(3)' : MyMessageIds.getNameByValue(3) } }, null, 4) + '</pre>';

2
  • It's good to use. Is there any way to get enum value as MyMessageIds.MoveIdCommentedApr 12, 2020 at 8:14
  • @sandeep.gosavi I think 'MyMessageIds.getValueByName('MoveId')' is gives what you want. Dont you want to use methods or what?CommentedDec 26, 2020 at 20:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.