Skip to content
Advertisement

Get index of enum from string?

I have a string value, I also have an array of strings and an enum containing the range also. To get the index of the string in the array, from the value supplied I write this:

JavaScript

How do I do this for an enum? Can I use ordinal? Or do i have to make my own method?

The method might go like:

JavaScript

Advertisement

Answer

Not sure if I understand you correctly but based on question title you may be looking for

JavaScript
  1. YourEnum.valueOf("VALUE") returns enum value with name "VALUE"
  2. each enum value knows its position (indexed from zero) which we can get by calling ordinal() method on it.
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement