我想迭代一个TypeScript枚举对象,并获得每个枚举符号名称,例如: enum myEnum {entry1, entry2}

for (var entry in myEnum) { 
    // use entry's name here, e.g., "entry1"
}

当前回答

他们在官方文件中提供了一个叫做“反向映射”的概念。它帮助了我:

https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings

解决方法很简单:

enum Enum {
 A,
}

let a = Enum.A;
let nameOfA = Enum[a]; // "A"

其他回答

根据TypeScript文档,我们可以通过Enum和静态函数来实现这一点。

使用静态函数获取Enum名称

enum myEnum { 
    entry1, 
    entry2 
}

namespace myEnum {
    export function GetmyEnumName(m: myEnum) {
      return myEnum[m];
    }
}


now we can call it like below
myEnum.GetmyEnumName(myEnum.entry1);
// result entry1 

要阅读更多关于Enum的静态函数,请点击下面的链接 https://basarat.gitbooks.io/typescript/docs/enums.html

假设您坚持使用规则,只生成带有数值的枚举,您可以使用这段代码。这正确地处理了名称恰好是有效数字的情况

enum Color {
    Red,
    Green,
    Blue,
    "10" // wat
}

var names: string[] = [];
for(var n in Color) {
    if(typeof Color[n] === 'number') names.push(n);
}
console.log(names); // ['Red', 'Green', 'Blue', '10']

假设您有一个枚举

export enum SCROLL_LABEL_OFFSET {
  SMALL = 48,
  REGULAR = 60,
  LARGE = 112
}

你想要创建一个基于枚举的类型而不仅仅是复制和粘贴。 你可以像这样使用枚举来创建你的类型:

export type ScrollLabelOffset = keyof typeof SCROLL_LABEL_OFFSET;

结果你会收到一个可能值为'SMALL' | 'REGULAR' | 'LARGE'的类型

使用当前版本的TypeScript,你可以使用这些函数将Enum映射到你选择的记录。注意,不能用这些函数定义字符串值,因为它们查找值为数字的键。

enum STATES {
  LOGIN,
  LOGOUT,
}

export const enumToRecordWithKeys = <E extends any>(enumeration: E): E => (
  Object.keys(enumeration)
    .filter(key => typeof enumeration[key] === 'number')
    .reduce((record, key) => ({...record, [key]: key }), {}) as E
);

export const enumToRecordWithValues = <E extends any>(enumeration: E): E => (
  Object.keys(enumeration)
    .filter(key => typeof enumeration[key] === 'number')
    .reduce((record, key) => ({...record, [key]: enumeration[key] }), {}) as E
);

const states = enumToRecordWithKeys(STATES)
const statesWithIndex = enumToRecordWithValues(STATES)

console.log(JSON.stringify({
  STATES,
  states,
  statesWithIndex,
}, null ,2));

// Console output:
{
  "STATES": {
    "0": "LOGIN",
    "1": "LOGOUT",
    "LOGIN": 0,
    "LOGOUT": 1
  },
  "states": {
    "LOGIN": "LOGIN",
    "LOGOUT": "LOGOUT"
  },
  "statesWithIndex": {
    "LOGIN": 0,
    "LOGOUT": 1
  }
}

简单地说

如果你的枚举如下:

export enum Colors1 {
  Red = 1,
  Green = 2,
  Blue = 3
}

要获得特定的文本和值:

console.log(Colors1.Red); // 1 
console.log(Colors1[Colors1.Red]); // Red

获取值和文本列表:

public getTextAndValues(e: { [s: number]: string }) {
  for (const enumMember in e) {
    if (parseInt(enumMember, 10) >= 0) {
      console.log(e[enumMember]) // Value, such as 1,2,3
      console.log(parseInt(enumMember, 10)) // Text, such as Red,Green,Blue
    }
  }
}
this.getTextAndValues(Colors1)

如果你的枚举如下:

export enum Colors2 {
  Red = "Red",
  Green = "Green",
  Blue = "Blue"
}

要获得特定的文本和值:

console.log(Colors2.Red); // Red
console.log(Colors2["Red"]); // Red

获取值和文本列表:

public getTextAndValues(e: { [s: string]: string }) {
  for (const enumMember in e) {
    console.log(e[enumMember]);// Value, such as Red,Green,Blue
    console.log(enumMember); //  Text, such as Red,Green,Blue
  }
}
this.getTextAndValues(Colors2)