Categories
JavaScript Answers

How to convert enums to array of values with JavaScript?

Spread the love

Sometimes, we want to convert enums to array of values with JavaScript.

In this article, we’ll look at how to convert enums to array of values with JavaScript.

How to convert enums to array of values with JavaScript?

To convert enums to array of values with JavaScript, we can use the Object.values method.

For instance, we write:

const types = {
  "WHITE": 0,
  "BLACK": 1
}
const vals = Object.values(types)
console.log(vals)

to call Object.values with types to return an array of the property values.

Therefore, vals is [0, 1].

Conclusion

To convert enums to array of values with JavaScript, 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 *