TypeScript enum to object array

前端 未结 15 2845
不思量自难忘°
不思量自难忘° 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:24

    If you are using ES6

    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.

提交回复
热议问题