Javascript 如何在ReactJs typescript中将多个枚举值传递给变量
我已将Javascript 如何在ReactJs typescript中将多个枚举值传递给变量,javascript,reactjs,typescript,enums,Javascript,Reactjs,Typescript,Enums,我已将COLORenum声明如下 export declare enum COLOR { RED = "RED", BLUE = "BLUE", YELLOW = "YELLOW" } interface IProps { config?: { disallowedColors: COLOR; }; 现在我需要访问值config.disallowedColors。如果我想将多种颜色传递到config.disallowedColors,我该怎么做??
COLOR
enum声明如下
export declare enum COLOR {
RED = "RED",
BLUE = "BLUE",
YELLOW = "YELLOW"
}
interface IProps {
config?: {
disallowedColors: COLOR;
};
现在我需要访问值config.disallowedColors
。如果我想将多种颜色传递到config.disallowedColors
,我该怎么做??
例如,我想要config.disallowedColors=red,yellow
有人能在这儿照点光吗 如果枚举值是字符串,一个简单的数组就足够了,即
config.disallowedColors=[COLOR.RED,COLOR.YELLOW]
检查时,您可以利用包括
/一些
等来检查是否设置了标志,例如:
config.disallowedColors.includes(COLOR.RED);
当您需要检查多个值时,它会变得稍微复杂一些,一个选项是为要检查的值创建一个临时数组,然后利用each
将每个值与目标数组进行比较,即
const flags = [COLOR.RED, COLOR.YELLOW];
const { disallowedColors } = config;
flags.every(c => disallowedColors.includes(c));
或者,如果您使用了一个数值,那么您可以利用它创建一个位掩码,它将给您相同的结果(只是以不同的方式),即
如果枚举值是字符串,那么一个简单的数组就足够了,即
config.disallowedColors=[COLOR.RED,COLOR.YELLOW]
检查时,您可以利用包括
/一些
等来检查是否设置了标志,例如:
config.disallowedColors.includes(COLOR.RED);
当您需要检查多个值时,它会变得稍微复杂一些,一个选项是为要检查的值创建一个临时数组,然后利用each
将每个值与目标数组进行比较,即
const flags = [COLOR.RED, COLOR.YELLOW];
const { disallowedColors } = config;
flags.every(c => disallowedColors.includes(c));
或者,如果您使用了一个数值,那么您可以利用它创建一个位掩码,它将给您相同的结果(只是以不同的方式),即
COLOR[]
或Array
?COLOR[]
或Array
?