Last updated: Feb 26, 2024
Reading timeยท7 min
If you need to get the length of an Enum, click on the following subheading:
To get all enum values as an array, pass the enum to the Object.values()
method.
The Object.values
method will return an array of the enum's values because
enums in TypeScript are real objects and exist at runtime.
// โ For STRING Enums enum StringEnum { Small = 'S', Medium = 'M', Large = 'L', } const values = Object.values(StringEnum); // ๐๏ธ ['S', 'M', 'L'] console.log(values); const names = Object.keys(StringEnum); // ๐๏ธ ['Small', 'Medium', 'Large'] console.log(names);
If you have a numeric enum, use the following code sample instead.
// โ For NUMERIC Enums enum NumericEnum { Small, Medium, Large, } const values = Object.keys(NumericEnum).filter((v) => !isNaN(Number(v))); console.log(values); // ๐๏ธ ['0', '1', '2'] const names = Object.keys(NumericEnum).filter((v) => isNaN(Number(v))); console.log(names); // ๐๏ธ ['Small', 'Medium', 'Large']
Enums are real objects in TypeScript and exist at runtime. This is why we are able to use the Object.values method to get an array containing the enum's values.
The Object.values()
method returns an array containing an object's values.
// ๐๏ธ ['bobby hadz', 30] console.log(Object.values({ name: 'bobby hadz', age: 30 }));
However, the output for numeric and string enums is different.
// โ For STRING Enums enum StringEnum { Small = 'S', Medium = 'M', Large = 'L', } // ๐๏ธ ['S', 'M', 'L'] console.log(Object.values(StringEnum)); // โ For NUMERIC Enums enum NumericEnum { Small, Medium, Large, } // ๐๏ธ ['Small', 'Medium', 'Large', 0, 1, 2] console.log(Object.values(NumericEnum));
Notice that when a numeric enum is passed to the Object.values()
method, the
enum's keys and values are returned.
We haven't provided an initial value for the numeric enum, so its values start
at 0
and auto-increment.
This is why we had to filter out the unnecessary keys in the examples.
If you need to get all enum names as an array:
Object.keys()
method.Object.keys()
method returns an array containing the
enum names.// โ STRING Enums enum StringEnum { Yes = 'Y', No = 'N', Maybe = 'M', } const names = Object.keys(StringEnum); console.log(names); // ๐๏ธ ['Yes', 'No', 'Maybe']
If you have a numeric enum, use the Array.filter() method.
// โ NUMERIC Enums enum NumericEnum { Yes, No, Maybe, } const names = Object.keys(NumericEnum).filter((v) => isNaN(Number(v))); console.log(names); // ๐๏ธ ['Yes', 'No', 'Maybe']
Enums in TypeScript are real objects and exist at runtime, so we are able to pass an enum to the Object.keys method.
The Object.keys()
method returns an array containing the object's keys.
// ๐๏ธ ['name', 'age'] console.log(Object.keys({ name: 'bobby hadz', age: 30 }));
However, the output for numeric and string enums is different.
// โ NUMERIC Enums enum NumericEnum { Yes, No, Maybe, } // โ STRING Enums enum StringEnum { Yes = 'Y', No = 'N', Maybe = 'M', } // ๐๏ธ ['0', '1', '2', 'Yes', 'No', 'Maybe'] console.log(Object.keys(NumericEnum)); // ๐๏ธ ['Yes', 'No', 'Maybe'] console.log(Object.keys(StringEnum));
Object.keys
method, we get an array containing the values and the names of the enum, whereas, for a string enum, we only get the names.This is why we used the filter()
method - to filter out the enum values from
the array.
If you need to iterate over enums, check out the following article.
The examples above wouldn't work if you use const
enums because const
enums
can only use constant enum expressions and are completely removed during
compilation.
To convert an enum to an array of objects:
Object.keys()
method to get an array of the enum's keys.map()
method to iterate over the array, returning an object on each
iteration.// โ For NUMERIC Enums enum NumericEnum { Small, Medium, Large, } const arr = Object.keys(NumericEnum) .filter((v) => isNaN(Number(v))) .map((name) => { return { id: NumericEnum[name as keyof typeof NumericEnum], name, }; }); // ๐๏ธ [{id: 0, name: 'Small'}, {id: 1, name: 'Medium'}, {id: 2, name: 'Large'}] console.log(arr);
If you have a string enum, use the following code sample instead.
// โ For STRING Enums enum StringEnum { Small = 'S', Medium = 'M', Large = 'L', } const arr = Object.keys(StringEnum).map((name) => { return { name, value: StringEnum[name as keyof typeof StringEnum], }; }); // ๐๏ธ [{name: 'Small', value: 'S'}, {name: 'Medium', value: 'M'}, {name: 'Large', value: 'L'}] console.log(arr);
The code sample shows how to convert numeric and string enums to an array of objects.
Enums in TypeScript are real objects and exist at runtime, so we are able to use the Object.keys method to get an array of the enum's keys.
// โ For NUMERIC Enums enum NumericEnum { Small, Medium, Large, } // ๐๏ธ ['0', '1', '2', 'Small', 'Medium', 'Large'] console.log(Object.keys(NumericEnum)); // โ For STRING Enums enum StringEnum { Small = 'S', Medium = 'M', Large = 'L', } // ๐๏ธ ['Small', 'Medium', 'Large'] console.log(Object.keys(StringEnum));
filter()
method to filter out any of the unnecessary values from the array.The last step is to use the Array.map method to loop over the array of enum names and return an object containing the name and value on each iteration.
keyof typeof
in the examples to cast the string to an enum, so we can access the corresponding value.// โ For STRING Enums enum StringEnum { Small = 'S', Medium = 'M', Large = 'L', } const arr = Object.keys(StringEnum).map((name) => { return { name, value: StringEnum[name as keyof typeof StringEnum], }; }); // ๐๏ธ [{name: 'Small', value: 'S'}, {name: 'Medium', value: 'M'}, {name: 'Large', value: 'L'}] console.log(arr);
The Array.map()
method returns an
array of objects where each object
has 2 key-value pairs - the name of the enum member and the corresponding value.
To get the length of an enum:
Object.keys()
method to get an array containing the enum's keys.length
property on the array.2
, because a reverse
mapping is generated.// โ For String Enums enum Sizes { Small = 'S', Medium = 'M', Large = 'L', } const len = Object.keys(Sizes).length; console.log(len); // ๐๏ธ 3
If you work with numeric enums, use the following code sample instead.
// โ For Numeric Enums enum SizesNumeric { Small, Medium, Large, } const len = Object.keys(SizesNumeric).length / 2; console.log(len); // ๐๏ธ 3
If your enum contains both string and numeric values, use the following code sample.
// โ For Mixed Enums (both strings and numbers) enum SizesMixed { Small = 1, Medium = 'test', Large = 3, ExtraLarge = 4, } const len = Object.keys(SizesMixed).filter((v) => isNaN(Number(v))).length; console.log(len); // ๐๏ธ 4
Enums in TypeScript are real objects and exist at runtime. This is why we are able to pass an enum to the Object.keys method.
The Object.keys()
method returns an array containing the object's keys.
// ๐๏ธ ['name', 'age'] console.log(Object.keys({ name: 'Bobby hadz', age: 30 }));
However, the output for numeric and string enums is different.
// โ For String Enums enum Sizes { Small = 'S', Medium = 'M', Large = 'L', } // ๐๏ธ ['Small', 'Medium', 'Large'] console.log(Object.keys(Sizes)); // โ For Numeric Enums enum SizesNumeric { Small, Medium, Large, } // ๐๏ธ ['0', '1', '2', 'Small', 'Medium', 'Large'] console.log(Object.keys(SizesNumeric));
Object.keys
method, we get an array containing the values and the names of the enum, whereas, for a string enum, we only get the names.This is because a reverse mapping gets generated only for numeric enums.
The reverse mapping allows us to access a numeric enum's key by a value.
// โ For Numeric Enums enum SizesNumeric { Small, Medium, Large, } console.log(SizesNumeric[0]); // ๐๏ธ "Small" console.log(SizesNumeric[1]); // ๐๏ธ "Medium"
This is why we had to divide the length of the array we got from the
Object.keys()
method by 2
for numeric enums.
// โ For Numeric Enums enum SizesNumeric { Small, Medium, Large, } const len = Object.keys(SizesNumeric).length / 2; console.log(len); // ๐๏ธ 3
String enum members don't get a reverse mapping generated at all.
If you work with mixed enums (ones that contain both string and numeric members), you have to filter out the keys for the reverse mapping.
In other words, we have to exclude the valid numbers from the array of keys.
// โ For Mixed Enums (both strings and numbers) enum SizesMixed { Small = 1, Medium = 'M', Large = 3, ExtraLarge = 4, } const length3 = Object.keys(SizesMixed).filter((v) => isNaN(Number(v))).length; console.log(length3); // ๐๏ธ 4
The filter()
method is there to filter out all of the keys in the array that
are valid numbers (the generated reverse mapping keys).
We don't have to divide by 2
because we've already excluded all of the keys
that were generated for the reverse mappings.
You can learn more about the related topics by checking out the following tutorials: