我努力找到一种方法来获取我的枚举变量名称和显示名称的字符串部分(使用变量名称和字符串'display'名称)
我想要这个,因为我会在我的过滤器查询中使用变量名称,并在前端显示显示名称。
所以我找到了一种创建一个对象来充当枚举的方法,并且想到id只是为你们添加它。
因此,不要创建枚举,只需以这种格式创建一个对象。
export let RewardCategory = {
swapPoints: {variable: 'swapPoints', display: 'Swap Points' },
charity: {variable: 'charity', display: 'Charity' },
discountVouchers: {variable: 'discountVouchers', display: 'Discount Vouchers' }
}
然后,包含thisEnum Layout。
export interface EnumLayout {
variable: string;
display: string;
}
然后,只需像这样使用它。
(<EnumLayout>RewardCategory['whateverstring']).display
要么
(<EnumLayout>RewardCategory['whateverstring']).variable
枚举被编码为普通的javascript对象,因此您可以执行以下操作:
enum Numbers {
one = 'number one',
two = 'the second number'
}
for (const key in Numbers)
console.log(`key: ${key}, value: ${Numbers[key]}`);
function getTheKeyFromTheValue(value: string) {
for (const key in Numbers)
if (Numbers[key] === value)
return key;
return undefined; // Couldn't find it
}