Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
}

>Solution :

you can use Object.values to check this

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'));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading