I have an enum in which I store some UI element values that look like this:
const uiElementAttributes = {
1: {
id: 1,
color: 'red',
icon: 'icon-something.png'
},
2: {
id: 2,
color: 'yellow',
icon: 'icon-something-else.png'
},
3: {
id: 3,
color: 'blue',
icon: 'icon-this-item.png'
},
99: {
id: 99,
color: 'black',
icon: 'icon-black.png'
}
};
I have a function that will return the correct object from the enum based on id passed to my function. My question is how do I check if a value is defined in an enum?
Here's the function:
const getAttributes (id) => {
// How do I check to see if the id is in the enum?
if(checkIfIdIsInEnum) {
return uiElementAttributes[id];
} else {
return uiElementAttributes[99];
}
};
UPDATE: I'm trying the suggested answer. It does look pretty straight forward but looks like when webpack transpiles my code to vanilla JS, it adds default
at the end of my enum
which is undefined
. Any idea what may be causing this -- see below? As you can see, the id
value I'm passing is 1
so it should be able to find the value 1 in my enum object.
And here's the actual enum
object:
Here's the actual function code:
UPDATE 2: I resolved the issue by importing the enum within curly braces -- see below:
import {calendarEvents} from '../../enums/calendarEvents';