I have the following enum
export enum Operators {
Equal = "1",
NotEqual = "2",
GreatherOrEqual = "3",
LessOrEqual = "4",
Contains = "5",
Null = "6",
NotNull = "7",
Between = "8",
StartsWith = "9",
EndsWith = "10"
}
I need to recover the enum key from the value that is stored. I have created the following:
GetEnumTextByValue ( valueOperator: string ): string {
const values: string[] = Object.values( Operators ).filter( v => v == v );
const keys: string[] = Object.keys( Operators ).filter( k => k == k );
const index: number = values.indexOf( valueOperator );
return keys[ index ];
}
The function returns what I need, but is there any simpler way to get the key?
Object.entries(Operators). It returns an array of [ key, value ]