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.