To get all enum values as an array, pass the enum to the Object. values() method, e.g. const values = Object. values(StringEnum) .
Use the map() method with Enums in TypeScript # To use the map() method with enums: Use the Object. keys() method to get an array of the enum's keys.
The error "Type string is not assignable to type Enum" occurs when we try to use a string literal in the place of an enum value. To solve the error, use dot or bracket notation to access the specific enum property or use a type assertion.
A tricky bit is that TypeScript will 'double' map the enum in the emitted object, so it can be accessed both by key and value.
enum MyEnum {
Part1 = 0,
Part2 = 1
}
will be emitted as
{
Part1: 0,
Part2: 1,
0: 'Part1',
1: 'Part2'
}
So you should filter the object first before mapping. So @Diullei 's solution has the right answer. Here is my implementation:
// Helper
const StringIsNumber = value => isNaN(Number(value)) === false;
// Turn enum into array
function ToArray(enumme) {
return Object.keys(enumme)
.filter(StringIsNumber)
.map(key => enumme[key]);
}
Use it like this:
export enum GoalProgressMeasurements {
Percentage,
Numeric_Target,
Completed_Tasks,
Average_Milestone_Progress,
Not_Measured
}
console.log(ToArray(GoalProgressMeasurements));
If you are using ES8
For this case only it will work perfectly fine. It will give you value array of the given enum.
enum Colors {
WHITE = 0,
BLACK = 1,
BLUE = 3
}
const colorValueArray = Object.values(Colors); //[ 'WHITE', 'BLACK', 'BLUE', 0, 1, 3 ]
You will get colorValueArray
like this [ 'WHITE', 'BLACK', 'BLUE', 0, 1, 3 ]
. All the keys will be in first half of the array and all the values in second half.
Even this kind of enum will work fine
enum Operation {
READ,
WRITE,
EXECUTE
}
But this solution will not work for Heterogeneous enums like this
enum BooleanLikeHeterogeneousEnum {
No = 0,
Yes = "YES",
}
Enums are real objects that exist at runtime. So you are able to reverse the mapping doing something like this:
let value = GoalProgressMeasurements.Not_Measured;
console.log(GoalProgressMeasurements[value]);
// => Not_Measured
Based on that you can use the following code:
export enum GoalProgressMeasurements {
Percentage = 1,
Numeric_Target = 2,
Completed_Tasks = 3,
Average_Milestone_Progress = 4,
Not_Measured = 5
}
let map: {id: number; name: string}[] = [];
for(var n in GoalProgressMeasurements) {
if (typeof GoalProgressMeasurements[n] === 'number') {
map.push({id: <any>GoalProgressMeasurements[n], name: n});
}
}
console.log(map);
Reference: https://www.typescriptlang.org/docs/handbook/enums.html
Simply this will return an array of enum values:
Object.values(myEnum);
Easy Solution. You can use the following function to convert your Enum to an array of objects.
buildGoalProgressMeasurementsArray(): Object[] {
return Object.keys(GoalProgressMeasurements)
.map(key => ({ id: GoalProgressMeasurements[key], name: key }))
}
If you needed to strip that underscore off, we could use regex as follows:
buildGoalProgressMeasurementsArray(): Object[] {
return Object.keys(GoalProgressMeasurements)
.map(key => ({ id: GoalProgressMeasurements[key], name: key.replace(/_/g, ' ') }))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With