[英]javascript enum to convert to list and check if the value is present in that enum
lets say i have an enum in javascript defined like this:假设我在 javascript 中有一个枚举定义如下:
const myEnum = {
A:'a',
B:'b',
C:'c',
D:'d'
};
I have a character and I need to check whether it is present in the enum or not.我有一个角色,我需要检查它是否存在于枚举中。
currently, I'm doing something like目前,我正在做类似的事情
if(value !== myEnum.A || value !==myEnum.B || ......) {
//FAILURE
}
this is something of a problem how do I make it something like:这是一个问题,我如何使它成为这样的东西:
if(value not in myEnum.values){
//FAILURE
}
you can use Object.values to check this你可以使用 Object.values 来检查这个
if (!Object.values(myEnum).includes(value)) {
// Do what you want here
}
const myEnum = { A: 'a', B: 'b', C: 'c', D: 'd' }; console.log(Object.values(myEnum).includes('a')); console.log(Object.values(myEnum).includes('v'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.