I receive a number type = 3
and have to check if it exists in this enum:
export const MESSAGE_TYPE = {
INFO: 1,
SUCCESS: 2,
WARNING: 3,
ERROR: 4,
};
The best way I found is by getting all Enum Values as an array and using indexOf on it. But the resulting code isn't very legible:
if( -1 < _.values( MESSAGE_TYPE ).indexOf( _.toInteger( type ) ) ) {
// do stuff ...
}
Is there a simpler way of doing this?
if(Object.values(MESSAGE_TYPE).includes(+type)
? There's not much you can do.!!MESSAGE_TYPE[type]
to check if a value exists.MESSAGE_TYPE[type]
will return undefined if the value oftype
doesn't exist onMESSAGE_TYPE
0
, though.MESSAGE_TYPE[type] !== undefined