Categories
TypeScript Answers

How to convert TypeScript enum to object array?

Spread the love

Sometimes, we want to convert TypeScript enum to object array.

In this article, we’ll look at how to convert TypeScript enum to object array.

How to convert TypeScript enum to object array?

To convert TypeScript enum to object array, we can use the Object.values method.

For instance, we write

enum Colors {
  WHITE = 0,
  BLACK = 1,
  BLUE = 3,
}

const colorValueArray = Object.values(Colors);

to create the Colors enum.

Then we call Object.values with Colors to return an array of enum values.

Conclusion

To convert TypeScript enum to object array, we can use the Object.values method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *