πŸ’‘ This page contain affiliate links. By making a purchase through them, we may earn a commission at no extra cost to you.

Two Ways to Get Enum Keys by Values in TypeScript

Ferenc Almasi β€’ 2022 September 28 β€’ Read time 2 min read
  • twitter
  • facebook
TypeScript

There are two ways to get enum keys by values in TypeScript. You can either use bracket notation or a combination of Object.values and Object.keys. Here is an example of using bracket notation:

Copied to clipboard!
enum Countries {
    Argentina = 'AR' as any,
    Armenia = 'AM' as any,
    Australia = 'AU' as any,
    Austria = 'AT' as any
}

// This will log "Argentina" to the console
console.log(Countries['AR'])
Getting enum keys by values using bracket notation

Notice that we typed all of them as any. It is important to type them, as otherwise, you will get undefined back. While the above works, it's a quick and dirty solution that can break in certain versions of TypeScript.

The other more reliable way to get enum keys by values is by using a combination of Object.values with Object.keys. Take a look at the following example:

Copied to clipboard!
enum Countries {
  Argentina = 'AR',
  Armenia = 'AM',
  Australia = 'AU',
  Austria = 'AT'
}

const keyIndex = Object.values(Countries).indexOf('AM')

// Returns "Armenia"
console.log(Object.keys(Countries)[keyIndex])
Getting enum keys by values using Object methods

This works by first getting the index of the value with "AM". This can be done by calling Object.values, and then getting the index of the value with the indexOf function.

Copied to clipboard!
Object.values(Countries)

// This will return the following array (and so keyIndex will be 1):
-> (4) ["AR", "AM", "AU", "AT"]

We can then use this index to grab the proper key by calling Object.keys, which again returns an array, but instead of returning the object values, it returns the keys that we need.

Copied to clipboard!
Object.values(Countries)

// This will return all keys associated with the enum
-> (4) ["Argentina", "Armenia", "Australia"...]

Creating a Helper Function for Getting Enum Keys

We can also create a helper function from the above code example to reuse it later if needed. Take a look at the following code:

Copied to clipboard!
const getKey = (obj, value) => {
    const keyIndex = Object.values(obj).indexOf(value)

    return Object.keys(obj)[keyIndex]
}

getKey(Countries, 'AR') // Returns "Argentina"
getKey(Countries, 'AM') // Returns "Armenia"
getKey(Countries, 'AU') // Returns "Australia"
getKey(Countries, 'AT') // Returns "Austria"
Creating a helper function for getting enum keys by values

This function expects an enum (or any object for that matter) and a value that you want to use to get the key. In fact, we can also use the same function with any other object to get the key as a string.

Copied to clipboard!
// We can also use the function on plain JavaScript objects too
const countries = {
  Argentina: 'AR',
  Armenia: 'AM',
  Australia: 'AU',
  Austria: 'AT'
}

// Returns "Australia"
getKey(countries, 'AU')
Using the helper function with a regular object
  • twitter
  • facebook
TypeScript
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.