Horje
How to Iterate over Enum Values in TypeScript?

Enums in TypeScript are a way to define a set of named constants. Sometimes we may need to iterate over all the values of an enum. There are several approaches to iterate over the enum values in TypeScript which are as follows:

Examples of Iterating over Enum Values in TypeScript

Using a for…in loop

In this approach, we are using a for…in loop to iterate over the keys of the enum and access the corresponding values. It is important to check if the property is a key of the enum to avoid iterating over any prototype properties.

Syntax:

for (const key in Enum) {
// Use 'value' as needed
}

Example: The below code uses for…in loop to iterate over enum values in typescript

JavaScript
enum StudentInfo {
    Name = "Yogesh",
    RollNo = "14",
    Branch = "Civil",
    Div = "A",
}

for (const key in StudentInfo) {
    if (StudentInfo.hasOwnProperty(key)) {
        console.log(StudentInfo[key]);
    }
}

Output:

Yogesh
14
Civil
A

Using Object.values()

In this approach, we will be using the Object.values() method. This method is used to extract the values of the enum. It returns an array containing the enum values, which can then be iterated over using array methods or loops.

Syntax:

const values = Object.values(YourEnum);
values.forEach(value => {
// Use 'value' as needed
});

Example: This example uses Object.values() to iterate over enum values in typescript.

JavaScript
enum GFG {
    Name = "GeeksForGeeks",
    Founder = "Sandeep Jain",
    Est = "2008"
}

const values = Object
    .values(GFG);
values
    .forEach(value => console.log(value));

Output:

GeeksForGeeks
Sandeep Jain
2008

Using a for…of loop

The for…of loop offers a streamlined approach to directly iterate over the values of an enum without the need for additional property checks. This loop provides a concise syntax for traversing enum values, enhancing code readability and maintainability.

Syntax:

for (const value of YourEnum) {
// Use 'value' as needed
}

Example:

JavaScript
enum Direction {
  North = "N",
  South = "S",
  East = "E",
  West = "W",
}

for (const value of Object.values(Direction)) {
  console.log(value);
}

Output:

N
S
E
W

Using Object.keys()

In this approach, we use the Object.keys() method. This method retrieves the keys of the enum as strings, which can then be mapped to their corresponding values.

Syntax:

const keys = Object.keys(YourEnum);
keys.forEach(key => {
// Use 'YourEnum[key as keyof typeof YourEnum]' as needed
});

Example:

JavaScript
enum Colors {
    Red = "RED",
    Green = "GREEN",
    Blue = "BLUE"
}

const keys = Object.keys(Colors);
keys.forEach(key => {
    console.log(Colors[key as keyof typeof Colors]);
});

Output:

RED
GREEN
BLUE



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
Find a Specific Pair in Matrix using JavaScript Find a Specific Pair in Matrix using JavaScript
How to show Image in an Alert Box using JavaScript ? How to show Image in an Alert Box using JavaScript ?
How to select DOM Elements in JavaScript ? How to select DOM Elements in JavaScript ?
How to Replace a JavaScript Alert Pop Up with a Fancy Alert Box ? How to Replace a JavaScript Alert Pop Up with a Fancy Alert Box ?
JavaScript Program to find Intersection of Unsorted Arrays JavaScript Program to find Intersection of Unsorted Arrays

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13