我想迭代一个TypeScript枚举对象,并获得每个枚举符号名称,例如: enum myEnum {entry1, entry2}
for (var entry in myEnum) {
// use entry's name here, e.g., "entry1"
}
我想迭代一个TypeScript枚举对象,并获得每个枚举符号名称,例如: enum myEnum {entry1, entry2}
for (var entry in myEnum) {
// use entry's name here, e.g., "entry1"
}
当前回答
在TypeScript中,一个enum在javascript中被编译成一个map(从键中获取值):
enum MyEnum {
entry0,
entry1,
}
console.log(MyEnum['entry0']); // 0
console.log(MyEnum['entry1']); // 1
它还创建了一个反向映射(从值中获取键):
console.log(MyEnum[0]); // 'entry0'
console.log(MyEnum[0]); // 'entry1'
所以你可以通过以下方式访问一个条目的名称:
console.log(MyEnum[MyEnum.entry0]); // 'entry0'
console.log(MyEnum[MyEnum.entry1]); // 'entry1'
但是,string enum在设计上没有反向映射(参见注释和pull request),因为这可能导致映射对象中的键和值之间的冲突。
enum MyEnum {
entry0 = 'value0',
entry1 = 'value1',
}
console.log(MyEnum['value0']); // undefined
console.log(MyEnum['value1']); // undefined
如果你想强制你的字符串enum编译一个反向映射(你必须确保所有的键和值都是不同的),你可以使用这个技巧:
enum MyEnum {
entry0 = <any>'value0',
entry1 = <any>'value1',
}
console.log(MyEnum['value0']); // 'entry0'
console.log(MyEnum['value1']); // 'entry1'
console.log(MyEnum[MyEnum.entry0]); // 'entry0'
console.log(MyEnum[MyEnum.entry1]); // 'entry1'
其他回答
当我遇到同样的问题时,你可以使用我写的enum-values包:
Git: enum-values
var names = EnumValues.getNames(myEnum);
这里有很多答案,尽管这是一个7年前的问题,但我还是查了一下,我猜会有更多的答案出现在这里。这是我的解决方案,它比其他解决方案简单一点,它处理数字/文本/混合值枚举,都是一样的。
enum funky {
yum , tum='tum', gum = 'jump', plum = 4
}
const list1 = Object.keys(funky)
.filter(k => (Number(k).toString() === Number.NaN.toString()));
console.log(JSON.stringify(list1)); // ["yum","tum","gum","plum"]"
// for the numeric enum vals (like yum = 0, plum = 4), typescript adds val = key implicitly (0 = yum, 4 = plum)
// hence we need to filter out such numeric keys (0 or 4)
Typescript游乐场示例
enum TransactionStatus {
SUBMITTED = 'submitted',
APPROVED = 'approved',
PAID = 'paid',
CANCELLED = 'cancelled',
DECLINED = 'declined',
PROCESSING = 'processing',
}
let set1 = Object.entries(TransactionStatus).filter(([,value]) => value === TransactionStatus.SUBMITTED || value === TransactionStatus.CANCELLED).map(([key,]) => {
return key
})
let set2 = Object.entries(TransactionStatus).filter(([,value]) => value === TransactionStatus.PAID || value === TransactionStatus.APPROVED).map(([key,]) => {
return key
})
let allKeys = Object.keys(TransactionStatus)
console.log({set1,set2,allKeys})
这对于基于键值的enum更有效:
enum yourEnum {
["First Key"] = "firstWordValue",
["Second Key"] = "secondWordValue"
}
Object.keys(yourEnum)[Object.values(yourEnum).findIndex(x => x === yourValue)]
// Result for passing values as yourValue
// FirstKey
// SecondKey
在TypeScript中,一个enum在javascript中被编译成一个map(从键中获取值):
enum MyEnum {
entry0,
entry1,
}
console.log(MyEnum['entry0']); // 0
console.log(MyEnum['entry1']); // 1
它还创建了一个反向映射(从值中获取键):
console.log(MyEnum[0]); // 'entry0'
console.log(MyEnum[0]); // 'entry1'
所以你可以通过以下方式访问一个条目的名称:
console.log(MyEnum[MyEnum.entry0]); // 'entry0'
console.log(MyEnum[MyEnum.entry1]); // 'entry1'
但是,string enum在设计上没有反向映射(参见注释和pull request),因为这可能导致映射对象中的键和值之间的冲突。
enum MyEnum {
entry0 = 'value0',
entry1 = 'value1',
}
console.log(MyEnum['value0']); // undefined
console.log(MyEnum['value1']); // undefined
如果你想强制你的字符串enum编译一个反向映射(你必须确保所有的键和值都是不同的),你可以使用这个技巧:
enum MyEnum {
entry0 = <any>'value0',
entry1 = <any>'value1',
}
console.log(MyEnum['value0']); // 'entry0'
console.log(MyEnum['value1']); // 'entry1'
console.log(MyEnum[MyEnum.entry0]); // 'entry0'
console.log(MyEnum[MyEnum.entry1]); // 'entry1'