How to get all enum values as an array
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Getting all enum values as an array is a common need when populating dropdowns, validating input, or iterating over every possible option. The approach varies by language because each language implements enums differently. In TypeScript, use Object.values() with filtering. In Java, call EnumType.values(). In Python, iterate the enum class directly. In C#, use Enum.GetValues(). In Swift, conform to CaseIterable. This article shows the idiomatic approach for each major language.
TypeScript / JavaScript
TypeScript enums compile to objects with both forward and reverse mappings for numeric enums, which requires filtering:
const enum Caveat
Java
Java enums have a built-in values() method generated by the compiler:
Enums with Fields
Python
Python's enum.Enum class is iterable, so you can convert it directly:
C#
Flags Enum
Swift
Swift enums use the CaseIterable protocol:
Enums with Associated Values
Kotlin
Common Pitfalls
- TypeScript numeric enums have reverse mappings:
Object.values(NumericEnum)returns both string keys and numeric values. You must filter by type to get only the values you want. String enums do not have this issue. - Java
values()creates a new array each call: Every call toEnumType.values()allocates a new array. In performance-critical code, cache the result in a static field rather than calling it repeatedly in loops. - C#
Enum.GetValuesreturnsArray, not a typed array: Before .NET 5,Enum.GetValues(typeof(T))returns an untypedArraythat must be cast. Use the genericEnum.GetValues<T>()on .NET 5+ for a typed result. - Swift
CaseIterabledoes not work with associated values: Enums with associated values (e.g.,case item(String)) cannot automatically conform toCaseIterable. You must provide a manualallCasesimplementation. - Python
Enumiteration order matches definition order: Members iterate in the order they are defined in the class body. This is guaranteed by the language (Python 3.6+), but relying on it for logic may confuse readers who expect alphabetical order.
Summary
| Language | Method | Return Type |
| TypeScript | Object.values(Enum) (filter for string enums) | string[] or number[] |
| Java | EnumType.values() | EnumType[] |
| Python | list(EnumClass) | list[EnumClass] |
| C# | Enum.GetValues<T>() | T[] |
| Swift | EnumType.allCases | [EnumType] |
| Kotlin | EnumType.entries | List<EnumType> |
Related reading
- How to get back Kafka producer and consumer configuration (Java API)?
- How to get bean using application context in spring boot
- How to get bearer token from header of a request in java spring boot?
- How to Get Current Pod in Kubernetes Java Application
- How to get current route name in react-navigation?
- How to get distinct values from an array of objects in JavaScript?
- How to get current time and date in Android
- How to get current timestamp in string format in Java? yyyy.MM.dd.HH.mm.ss

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.