Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get names of enum entries?

I would like to iterate a TypeScript enum object and get each enumerated symbol name, for example: enum myEnum { entry1, entry2 }

for (var entry in myEnum) {      // use entry's name here, e.g., "entry1" } 
like image 925
CalvinDale Avatar asked Aug 07 '13 19:08

CalvinDale


People also ask

How do I get a list of enum names?

Use a list comprehension to get a list of all enum values, e.g. values = [member. value for member in Sizes] . On each iteration, access the value attribute on the enum member to get a list of all of the enum's values.

How do I get all the enum values in typescript?

To get all enum values as an array, pass the enum to the Object. values() method, e.g. const values = Object. values(StringEnum) . The Object.

How can enum get string name?

SOLUTION: int enumValue = 2; // The value for which you want to get string string enumName = Enum. GetName(typeof(EnumDisplayStatus), enumValue);


1 Answers

Though the answer is already provided, Almost no one pointed to the docs

Here's a snippet

enum Enum {     A } let nameOfA = Enum[Enum.A]; // "A" 

Keep in mind that string enum members do not get a reverse mapping generated at all.

like image 89
shakram02 Avatar answered Sep 18 '22 01:09

shakram02