我想迭代一个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"
}
当前回答
我希望这个问题仍然有意义。我使用这样的函数:
function enumKeys(target: Record<string, number|string>): string[] {
const allKeys: string[] = Object.keys(target);
const parsedKeys: string[] = [];
for (const key of allKeys) {
const needToIgnore: boolean
= target[target[key]]?.toString() === key && !isNaN(parseInt(key));
if (!needToIgnore) {
parsedKeys.push(key);
}
}
return parsedKeys;
}
function enumValues(target: Record<string, number|string>): Array<string|number> {
const keys: string[] = enumKeys(target);
const values: Array<string|number> = [];
for (const key of keys) {
values.push(target[key]);
}
return values;
}
例子:
enum HttpStatus {
OK,
INTERNAL_ERROR,
FORBIDDEN = 'FORBIDDEN',
NOT_FOUND = 404,
BAD_GATEWAY = 'bad-gateway'
}
console.log(enumKeys(HttpStatus));
// > ["OK", "INTERNAL_ERROR", "FORBIDDEN", "NOT_FOUND", "BAD_GATEWAY"]
console.log(enumValues(HttpStatus));
// > [0, 1, "FORBIDDEN", 404, "bad-gateway"]
其他回答
对我来说,一个更简单、实用和直接的方法来理解正在发生的事情,就是下面的列举:
enum colors { red, green, blue };
本质上将转换为:
var colors = { red: 0, green: 1, blue: 2,
[0]: "red", [1]: "green", [2]: "blue" }
正因为如此,以下情况将是正确的:
colors.red === 0
colors[colors.red] === "red"
colors["red"] === 0
这创建了一个简单的方法来获取枚举的名称,如下所示:
var color: colors = colors.red;
console.log("The color selected is " + colors[color]);
它还创建了一种将字符串转换为枚举值的好方法。
var colorName: string = "green";
var color: colors = colors.red;
if (colorName in colors) color = colors[colorName];
以上两种情况更为常见,因为通常您对特定值的名称和以通用方式序列化值更感兴趣。
如果它是你的枚举,你定义如下所示,名称和值是相同的,它会直接给你条目的名称。
enum myEnum {
entry1="entry1",
entry2="entry2"
}
for (var entry in myEnum) {
// use entry's name here, e.g., "entry1"
}
简单地说
如果你的枚举如下:
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)
我希望这个问题仍然有意义。我使用这样的函数:
function enumKeys(target: Record<string, number|string>): string[] {
const allKeys: string[] = Object.keys(target);
const parsedKeys: string[] = [];
for (const key of allKeys) {
const needToIgnore: boolean
= target[target[key]]?.toString() === key && !isNaN(parseInt(key));
if (!needToIgnore) {
parsedKeys.push(key);
}
}
return parsedKeys;
}
function enumValues(target: Record<string, number|string>): Array<string|number> {
const keys: string[] = enumKeys(target);
const values: Array<string|number> = [];
for (const key of keys) {
values.push(target[key]);
}
return values;
}
例子:
enum HttpStatus {
OK,
INTERNAL_ERROR,
FORBIDDEN = 'FORBIDDEN',
NOT_FOUND = 404,
BAD_GATEWAY = 'bad-gateway'
}
console.log(enumKeys(HttpStatus));
// > ["OK", "INTERNAL_ERROR", "FORBIDDEN", "NOT_FOUND", "BAD_GATEWAY"]
console.log(enumValues(HttpStatus));
// > [0, 1, "FORBIDDEN", 404, "bad-gateway"]
从TypeScript 2.4开始,枚举可以包含字符串初始化器https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-4.html
这允许你这样写:
enum Order {
ONE = "First",
TWO = "Second"
}
console.log(`One is ${Order.ONE.toString()}`);
得到这样的输出:
一个是First