Last updated: Feb 26, 2024
Reading timeยท3 min
To iterate over enums:
Object.keys()
or Object.values()
methods to get an array of the
enum's keys or values.forEach()
method to iterate over the array.// โ For STRING Enums enum Sizes { Small = 'S', Medium = 'M', Large = 'L', } const keys = Object.keys(Sizes); console.log(keys); // ๐๏ธ ['Small', 'Medium', 'Large'] keys.forEach((key, index) => { // ๐๏ธ Small, Medium, Large console.log(key); }); const values = Object.values(Sizes); values.forEach((value, index) => { // ๐๏ธ S, M, L console.log(value); });
If you work with numeric enums, use the following code sample instead.
// โ For Numeric Enums enum Sizes { Small, Medium, Large, } const keys = Object.keys(Sizes).filter((v) => isNaN(Number(v))); console.log(keys); // ๐๏ธ ['Small', 'Medium', 'Large'] keys.forEach((key, index) => { // ๐๏ธ Small, Medium, Large console.log(key); }); const values = Object.values(Sizes).filter((v) => !isNaN(Number(v))); console.log(values); // ๐๏ธ [0, 1, 2] values.forEach((value) => { // ๐๏ธ 0, 1, 2 console.log(value); });
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 an array containing the
object's keys and values.
const obj = { name: 'bobby hadz', age: 30 }; const keys = Object.keys(obj); console.log(keys); // ๐๏ธ ['name', 'age'] const values = Object.values(obj); console.log(values); // ๐๏ธ ['bobby hadz', 30]
However, the output for numeric enums is different.
enum Sizes2 { Small, Medium, Large, } // ๐๏ธ ['0', '1', '2', 'Small', 'Medium', 'Large'] console.log(Object.keys(Sizes2));
The output contains both the enum names and values. This is why we used the
filter()
method in the examples above - to filter out any unnecessary values
from the array.
I've also written a detailed guide on how to use the map() method with Enums.
for...of
loopThe examples above use the
Array.forEach
method to iterate over the enum keys and values. However, you could also use a
simple for...of
loop.
// โ For STRING Enums enum Sizes { Small = 'S', Medium = 'M', Large = 'L', } const keys = Object.keys(Sizes); console.log(keys); // ๐๏ธ ['Small', 'Medium', 'Large'] for (const key of keys) { console.log(key); } const values = Object.values(Sizes); console.log(values); // ๐๏ธ [ 'S', 'M', 'L' ] for (const value of values) { console.log(value); }
Use the following code sample if you work with numeric enums.
enum Sizes { Small, Medium, Large, } const keys = Object.keys(Sizes).filter((v) => isNaN(Number(v))); console.log(keys); // ๐๏ธ ['Small', 'Medium', 'Large'] for (const key of keys) { console.log(key); } const values = Object.values(Sizes).filter((v) => !isNaN(Number(v))); console.log(values); // ๐๏ธ [0, 1, 2] for (const value of values) { console.log(value); }
The syntax for a for...of
loop is a little more concise but you don't have
access to the index
like you do when using the forEach()
method.
You might have seen a for...in
loop being used to directly iterate over an
enum.
enum Sizes { Small, Medium, Large, } for (const value in Sizes) { // ๐๏ธ 0, 1, 2, Small, Medium, Large console.log(value); }
However, this way you iterate over both - the enum values and keys, which is most likely not what you want.
The examples above wouldn't work if you use const enums.
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: