TypeScript enum to object array

前端 未结 15 2910
不思量自难忘°
不思量自难忘° 2020-12-08 03:38

I have an enum defined this way:

export enum GoalProgressMeasurements {
    Percentage = 1,
    Numeric_Target = 2,
    Completed_Tasks = 3,
    Average_Mile         


        
15条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 04:14

    First we get an array of keys for this enum. Then, using the map () function, we convert the data to the desired format. id is obtained from the key, name is obtained from enum by the same key.

    const converted = Object.keys(GoalProgressMeasurements).map(key => {
            return {
                id: GoalProgressMeasurements[key],
                name: key,
            };
        });
    

提交回复
热议问题