Last updated: Feb 26, 2024
Reading timeยท4 min
To get an enum key by value:
Object.values()
method to get an array of the enum's values.indexOf()
method to get the index of the value in the array.Object.keys()
method to get an array of the enum's keys.// โ For String Enums enum Sizes { Small = 'S', Medium = 'M', Large = 'L', } const indexOfS = Object.values(Sizes).indexOf('S' as unknown as Sizes); const key = Object.keys(Sizes)[indexOfS]; console.log(key); // ๐๏ธ "Small"
If you work with numeric enums, use the following code sample instead.
// โ For Numeric Enums enum SizesNumeric { Small, Medium, Large, } console.log(SizesNumeric[0]); // ๐๏ธ "Small" console.log(SizesNumeric[1]); // ๐๏ธ "Medium"
TypeScript enums are real objects and exist at runtime. This is why we are able to pass an enum to the Object.keys and Object.values methods.
The Object.keys()
and Object.values()
methods return arrays containing the
object's keys and values.
enum Sizes { Small = 'S', Medium = 'M', Large = 'L', } // ๐๏ธ ['Small', 'Medium', 'Large'] console.log(Object.keys(Sizes)); // ๐๏ธ ['S', 'M', 'L'] console.log(Object.values(Sizes));
We used the indexOf
method to get the index of the specific value in the array
of values, and then used bracket notation to access the array of keys at the
specific index.
If you have to use this approach multiple times, create a reusable function.
enum Sizes { Small = 'S', Medium = 'M', Large = 'L', } function getKeyByValue(value: string) { const indexOfS = Object.values(Sizes).indexOf(value as unknown as Sizes); const key = Object.keys(Sizes)[indexOfS]; return key; } console.log(getKeyByValue('S')); // ๐๏ธ Small console.log(getKeyByValue('M')); // ๐๏ธ Medium
If you have to get the key of a numeric enum by a value, use reverse mappings.
// โ For Numeric Enums enum SizesNumeric { Small, Medium, Large, } console.log(SizesNumeric[0]); // ๐๏ธ "Small" console.log(SizesNumeric[1]); // ๐๏ธ "Medium" console.log(SizesNumeric[2]); // ๐๏ธ "Large"
Unfortunately, you can only use this approach to get the names of numeric
enums.
Note that the examples from the article would not work if you use const enums.
Const enums can only use constant enum expressions and are completely removed during compilation.
If you need to check if a value exists in an enum, check out the following article.
To access an enum by index:
Object.values()
method to get an array containing the enum's
values.// โ For STRING Enums enum Sizes { Small = 'S', Medium = 'M', Large = 'L', } const indexOfS = Object.keys(Sizes).indexOf('Small'); console.log(indexOfS); const s = Object.values(Sizes)[indexOfS]; console.log(s); // ๐๏ธ "S" const indexOfM = Object.keys(Sizes).indexOf('Medium'); console.log(indexOfM); // ๐๏ธ 1 const m = Object.values(Sizes)[indexOfM]; console.log(m); // ๐๏ธ "M"
If you work with numeric enums, use the following code sample instead.
// โ For Numeric Enums enum NumericEnum { Small, Medium, Large, } const s1 = NumericEnum[0]; console.log(s1); // ๐๏ธ Small const m1 = NumericEnum[1]; console.log(m1); // ๐๏ธ Medium
Enums in TypeScript are real objects and exist at runtime. This is why we are able to pass an enum to the Object.keys and Object.values methods.
The Object.keys()
method returns an array containing the keys of the enum and
the Object.values()
method - an array containing the enum's values.
// โ For STRING Enums enum Sizes { Small = 'S', Medium = 'M', Large = 'L', } console.log(Object.keys(Sizes)); // ๐๏ธ ['Small', 'Medium', 'Large'] console.log(Object.values(Sizes)); // ๐๏ธ ['S', 'M', 'L']
Now we can use an index to access the specific key or value of the enum.
When used with numeric enums, the Object.keys()
and Object.values()
methods
return an array containing both the enum's keys and values, so if you need just
one or the other, you need to filter out the unnecessary values.
// โ For Numeric Enums enum NumericEnum { Small, Medium, Large, } const names = Object.keys(NumericEnum).filter((v) => isNaN(Number(v))); console.log(names); // ๐๏ธ ['Small', 'Medium', 'Large'] const values = Object.values(NumericEnum).filter((v) => !isNaN(Number(v))); console.log(values); // ๐๏ธ [0, 1, 2]
However, when working with numeric enums, you can use reverse mappings to get an enum's key by its value.
// โ For Numeric Enums enum NumericEnum { Small, Medium, Large, } const s1 = NumericEnum[0]; console.log(s1); // ๐๏ธ "Small" const m1 = NumericEnum[1]; console.log(m1); // ๐๏ธ "Medium"
Unfortunately, you can only use this approach to get the names of numeric enums.
String enum members don't get a reverse mapping generated at all.
If your numeric enums have a different initial value than 0
, you can pass the
enum to the Object.values()
method and access the specific index.
enum NumericEnum { Small = 1, Medium, Large, } const s1 = Object.values(NumericEnum)[0]; console.log(s1); // ๐๏ธ "Small" const m1 = Object.values(NumericEnum)[1]; console.log(m1); // ๐๏ธ "Medium"
Even though the enum in the example has an initial value of 1
, we are able to
access the pair at index 0
by using the Object.values()
method.
enum NumericEnum { Small = 1, Medium, Large, } // ๐๏ธ ['Small', 'Medium', 'Large', 1, 2, 3] console.log(Object.values(NumericEnum));
Note that the examples above would not work if you use const enums, because const enums can only use constant enum expressions and are completely removed during compilation.
You can learn more about the related topics by checking out the following tutorials: