In my enum, I store various UI element values organized as follows:
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'
}
};
My function is designed to retrieve the correct object from the enum based on the provided id. My inquiry revolves around determining how to ascertain if a value exists within the enum.
Below is the function in question:
const getAttributes = (id) => {
// How can I validate if the id exists in the enum?
if(checkIfIdIsInEnum) {
return uiElementAttributes[id];
} else {
return uiElementAttributes[99];
}
};
UPDATE: I have implemented the recommended solution. Although it initially appeared straightforward, a peculiar scenario arises when webpack transpiles my code to vanilla JS, appending 'default' at the end of my 'enum' leading to 'undefined'. Any insights on what might be triggering this issue -- exemplified below? The 'id' value being passed is '1', which should correspond to the value '1' within my enum object.
https://i.sstatic.net/QYRCm.png
Here is the exact 'enum' object snapshot: https://i.sstatic.net/Wlz1H.png
Actual function code snippet provided below: https://i.sstatic.net/njjha.png
UPDATE 2: I managed to resolve the issue by importing the enum within curly braces -- detailed below:
import {calendarEvents} from '../../enums/calendarEvents';