I have a enum object and how to get the object value when passing the key using typescript
Below switch case works what if the enum object is so long, any other better way to do
export enum AllGroup = {
'GROUP_AUS': 'A'
'GROUP_IN': 'B'
}
var input = 'GROUP_IN'
Expected Output
B
Tried
const renderValue = (input: string): JSX.Element => {
switch (input) {
case AllGroup.GROUP_AUS:
return <>A</>;
case AllGroup.GROUP_IN:
return <>B</>;
}
return <div></div>;
};
2
Answers
You can directly access the enum by the key:
If you want a more formalized approach, you can create a function that returns an object with methods for getting values by keys or returns all values. In the below case
get()
andgetAll()
.Refer the below code for reference :