Last updated: Feb 26, 2024
Reading timeยท3 min
To use the map()
method with enums:
Object.keys()
method to get an array of the enum's keys.map()
method to iterate over the array.// โ For String Enums enum Sizes { Small = 'S', Medium = 'M', Large = 'L', } const result = (Object.keys(Sizes) as (keyof typeof Sizes)[]).map( (key, index) => { console.log(key); return Sizes[key] + index; }, ); // ๐๏ธ ['S0', 'M1', 'L2'] console.log(result);
If you work with numeric enums, use the following code sample instead.
// โ For Numeric Enums enum SizesNumeric { Small, Medium, Large, } const result = ( Object.keys(SizesNumeric).filter((v) => isNaN(Number(v)), ) as (keyof typeof SizesNumeric)[] ).map((key, index) => { return SizesNumeric[key]; }); // ๐๏ธ [0, 1, 2] console.log(result);
The examples show how to use the Array.map method with string and 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 method.
The Object.keys()
method returns an array containing the object's keys.
// ๐๏ธ ['name', 'age'] console.log(Object.keys({ name: 'Jim', age: 31 }));
However, the output for numeric and string enums is different.
enum Sizes { Small = 'S', Medium = 'M', Large = 'L', } // ๐๏ธ ['Small', 'Medium', 'Large'] console.log(Object.keys(Sizes)); // โ For String Enums enum SizesNumeric { Small, Medium, Large, } // ๐๏ธ ['0', '1', '2', 'Small', 'Medium', 'Large'] console.log(Object.keys(SizesNumeric));
Note that for numeric enums, the output contains the keys and values.
This is why we used the filter()
method in the first example - to exclude all
of the unnecessary values from the array.
We used keyof typeof to cast the values in the array as enum members.
enum Sizes { Small = 'S', Medium = 'M', Large = 'L', } // ๐๏ธ type U = "Small" | "Medium" | Large type U = keyof typeof Sizes;
Keyof typeof constructs a type that represents all of the keys in the enum as strings.
The final step is to use the Array.map()
method.
map()
method gets invoked with each value in the array and whatever we return from the callback function will get added to the new array.In other words, to use the map()
method with an enum, we have to convert the
enum keys or values to an array and call the map()
method on the result.
I've also written a guide on how to
iterate over Enums using different methods such
as forEach()
or a for...of
loop.
If you want to map over the values of an enum directly, you can get an array of an enum's values in the following ways.
enum Sizes { Small = 'S', Medium = 'M', Large = 'L', } const keys1 = Object.keys(Sizes); console.log(keys1); // ๐๏ธ ['Small', 'Medium', 'Large'] const values1 = Object.values(Sizes); console.log(values1); // ๐๏ธ ['S', 'M', 'L']
If you work with numeric enums, use the following code sample instead.
enum SizesNumeric { Small, Medium, Large, } const keys = Object.keys(SizesNumeric).filter((v) => isNaN(Number(v))); console.log(keys); // ๐๏ธ ['Small', 'Medium', 'Large'] const values = Object.values(SizesNumeric).filter((v) => !isNaN(Number(v))); console.log(values); // ๐๏ธ [0, 1, 2]
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: