简体   繁体   中英

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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