简体   繁体   English

如何在给定键名的情况下从枚举中获取值

[英]How to get value from enumeration given the name of the key

i have the below mentioned enumration.我有下面提到的枚举。 i know how to value of a color in the enumeration by specifying its index as follows:我知道如何通过指定其索引来评估枚举中颜色的值,如下所示:

Object.values(ColorEnum)[index]

what i want to know is how to get value stored in an enumeration given the key?in other words, how to get the values, for example, #FF0B0B by specifying the key RED我想知道的是如何在给定键的情况下获取存储在枚举中的值?换句话说,如何通过指定键RED来获取值,例如#FF0B0B

code :代码

export enum ColorEnum {
RED = "#FF0B0B",
RADICAL_RED = "2ff4357",
BRINK_POINK = "#fa6e8a",
PURPLE_PIZZAZZ = "#FF0BD1",

YELLOW = "#FFEB3B",
//RED2 = "#C62828",
GREEN = "#4CAF50",
//PINK2 = "#AD1457",
PURPLE = "#9C27B0",
//PURPLE2 = "#6A1B9A",
DEEP_PURPLE = "#4527A0",
INDIGO = "#536DFE",
BLUE = "#2196F3",

Assuming "RED" is in a string variable, you can get the enum's value using:假设“RED”在字符串变量中,您可以使用以下方法获取枚举值:

let color = "RED";
let colorCode = Color[color];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM