TypeScript enum to object array
2022-08-30 01:36:09
我有一个以这种方式定义的枚举:
export enum GoalProgressMeasurements {
Percentage = 1,
Numeric_Target = 2,
Completed_Tasks = 3,
Average_Milestone_Progress = 4,
Not_Measured = 5
}
但是,我希望它表示为来自我们的API的对象数组/列表,如下所示:
[{id: 1, name: 'Percentage'},
{id: 2, name: 'Numeric Target'},
{id: 3, name: 'Completed Tasks'},
{id: 4, name: 'Average Milestone Progress'},
{id: 5, name: 'Not Measured'}]
有没有简单和原生的方法来做到这一点,或者我是否必须构建一个函数,将枚举同时转换为int和字符串,并将对象构建到数组中?