Reputation: 163
I have the following enum:
export enum Categoria {
Acao = 1,
Opcao = 2,
FundoImobiliario = 3,
ContratoFuturo = 4,
ETF = 5,
BDR = 6
}
I use the following code to return the enum members to the screen:
export function enumSelector(objeto: any) {
return Object.keys(objeto).filter(key => !isNaN(Number(objeto[key])));
}
This enum populates an HTML select. So I select an option and do the post on my form, but what I get is the member name enum. How do I get the enum index? For example: 1 or 2 or 3 etc.
Upvotes: 2
Views: 81
Reputation: 185
This code may help you with the issue you are handling (keyof)
enum socialMedia {
Instagram = 1,
Facebook,
Whatsapp,
Snapchat
}
type KeyofEnum = keyof socialMedia;
function getsocialMedia(mediaOfficial: string): socialMedia {
if (mediaOfficial === 'Filters' || mediaOfficial === 'Snaps') {
return socialMedia.Snapchat;
}
}
let mediaType: socialMedia = getsocialMedia('Snaps'); // returns Snapchat
console.log('keyof enum string type Snapchat is', mediaType);
Output:
TypeScript keyof Enum 1
Upvotes: 1