Sometimes, we want to convert a TypeScript enum to object array.
In this article, we’ll look at how to convert a TypeScript enum to object array.
How to convert a TypeScript enum to object array?
To convert a TypeScript enum to object array, we can use the Object.values
method.
For instance, we write
enum Colors {
WHITE = 0,
BLACK = 1,
BLUE = 2,
}
const colorValueArray = Object.values(Colors);
to call Object.values
with the Colors
enum.
This will return an array with the values that we assigned to each enum entry.
So colorValueArray
is [0, 1, 2]
.
Conclusion
To convert a TypeScript enum to object array, we can use the Object.values
method.