具有多个字符串值的 typescript 枚举

标签 typescript enums

我努力找到一种方法来获取我的枚举变量名称和显示名称的字符串部分(同时使用变量名称和字符串“显示”名称)

我想要这个,因为我会在我的过滤器查询中使用变量名称,并在前端显示显示名称。

所以我找到了一种方法来创建一个对象来充当枚举,并认为 id 只是在这里为你们添加它。

最佳答案

因此,不要创建枚举,只需以这种格式创建一个对象。

export const RewardCategory = { 
  swapPoints:  {variable: 'swapPoints', display: 'Swap Points' },
  charity: {variable: 'charity', display: 'Charity' },
  discountVouchers: {variable: 'discountVouchers', display: 'Discount Vouchers' }
}
然后,像这样简单地使用它。
RewardCategory.swapPoints.display 
或者
 RewardCategory.swapPoints.variable

关于具有多个字符串值的 typescript 枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52200963/

相关文章:

c - typedef 音符枚举,制作将频率映射到音符的静态数组

typescript - 使用 import() 而不是 require() 来响应 native 图像导入

javascript - Typescript 检查 "any n consecutive elements are same"是否在 M 大小的数组中

typescript - 如何在 typescript 中使用枚举作为索引键类型?

c++ - C++ 中的灵活枚举

java - 反序列化 Enum,其中包含 Map

node.js - typescript 错误 TS2339 : Property 'id' does not exist on type ' User'

typescript - 为 CDN 使用类型

Angular 2 - 从响应中获取 cookie

Python Enum 在对成员值使用相同的字典时显示奇怪的行为