Get an Enum Key by Value in TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 26, 2024
4 min

banner

# Table of Contents

  1. Get an Enum Key by Value in TypeScript
  2. Access an Enum by Index in TypeScript

# Get an Enum Key by Value in TypeScript

To get an enum key by value:

  1. Use the Object.values() method to get an array of the enum's values.
  2. Use the indexOf() method to get the index of the value in the array.
  3. Use the Object.keys() method to get an array of the enum's keys.
  4. Access the array of keys at the specific index.
index.ts
// โœ… 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"

get enum key by value in typescript

The code for this article is available on GitHub

If you work with numeric enums, use the following code sample instead.

index.ts
// โœ… For Numeric Enums enum SizesNumeric { Small, Medium, Large, } console.log(SizesNumeric[0]); // ๐Ÿ‘‰๏ธ "Small" console.log(SizesNumeric[1]); // ๐Ÿ‘‰๏ธ "Medium"

get enum key by value in numeric enum

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.

index.ts
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.

# Creating a reusable function

If you have to use this approach multiple times, create a reusable function.

index.ts
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

creating reusable function

The code for this article is available on GitHub

# Get the key of a numeric enum by value

If you have to get the key of a numeric enum by a value, use reverse mappings.

index.ts
// โœ… For Numeric Enums enum SizesNumeric { Small, Medium, Large, } console.log(SizesNumeric[0]); // ๐Ÿ‘‰๏ธ "Small" console.log(SizesNumeric[1]); // ๐Ÿ‘‰๏ธ "Medium" console.log(SizesNumeric[2]); // ๐Ÿ‘‰๏ธ "Large"

use reverse mappings with numeric enums

The code for this article is available on GitHub

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.

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.

# Access an Enum by Index in TypeScript

To access an enum by index:

  1. Use the Object.values() method to get an array containing the enum's values.
  2. Use square brackets to access the array at the specific index and get the value.
index.ts
// โœ… 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"

access enum by index in typescript

The code for this article is available on GitHub

If you work with numeric enums, use the following code sample instead.

index.ts
// โœ… For Numeric Enums enum NumericEnum { Small, Medium, Large, } const s1 = NumericEnum[0]; console.log(s1); // ๐Ÿ‘‰๏ธ Small const m1 = NumericEnum[1]; console.log(m1); // ๐Ÿ‘‰๏ธ Medium

access enum by index in numeric enums

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.

index.ts
// โœ… 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.

index.ts
// โœ… 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]

# Access a numeric enum by index

However, when working with numeric enums, you can use reverse mappings to get an enum's key by its value.

index.ts
// โœ… For Numeric Enums enum NumericEnum { Small, Medium, Large, } const s1 = NumericEnum[0]; console.log(s1); // ๐Ÿ‘‰๏ธ "Small" const m1 = NumericEnum[1]; console.log(m1); // ๐Ÿ‘‰๏ธ "Medium"
The code for this article is available on GitHub

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.

index.ts
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.

index.ts
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev