2

I have a string enum:

export enum TokenLength {
  SIX = '6',
  EIGHT = '8',
}

And I need to get the string 'SIX' oder 'EIGHT' so reverse map this enum. I've tried multiple things:

tokenLength = TokenLength;      

for (var enumMember in this.tokenLength) {
    console.log("enum member: ", enumMember); //result: enum member:  SIX , enum member:  EIGHT
}

for (var enumMember in this.tokenLength) {
    console.log("enum member: ", this.tokenLength[enumMember]); //result: enum member:  6, enum member:  8
}

This seems to be working just fine but whenever I do the following it does not work.

console.log(this.tokenLength['SIX']); //result: 6
console.log(this.tokenLength.SIX); //result: 6
console.log(this.tokenLength[this.tokenLength.SIX]); //result: undefined
console.log(this.tokenLength['6']); //result: undefined
console.log(this.tokenLength[0]); //result: undefined

I need the 'undefined' result to be 'SIX' How can I do that? Thanks in advance.

Anaa
  • 1,191
  • 1
  • 9
  • 21
  • 1
    Possible duplicate of [How does one get the names of TypeScript enum entries?](https://stackoverflow.com/questions/18111657/how-does-one-get-the-names-of-typescript-enum-entries) –  Apr 09 '18 at 13:55
  • @trichetriche It probably is, but it does not seem to work the same for me, maybe because its a string enum and not a simple enum. – Anaa Apr 10 '18 at 06:38
  • Instead of saying it doesn't work, try it. https://stackblitz.com/edit/angular-znqurt –  Apr 10 '18 at 06:41
  • @trichetriche Well look at my post. The snippet you sent works fine. But I don't want to iterate through my enum. I want a direct access to the string 'SIX'. – Anaa Apr 10 '18 at 07:53
  • You can't. Enums are Javascript objects. If yo uwant a reference to the key of your object, you need to know what you're looking for. This means that you have to know the key. Look at my snippet, in the console, and you will understand. –  Apr 10 '18 at 08:00

4 Answers4

0

You can look at typescript documentation.typescript

 enum Enum {
        A
  }
  let a = Enum.A;
  let nameOfA = Enum[a]; // "A"

To make it work this way you have to remove quotes in your enum:

enum TokenLength {
      SIX = 6,
      EIGHT = 8
}

Hope that helps

kimy82
  • 4,069
  • 1
  • 22
  • 25
  • I tried that exact code and the result is in fact 'A'. But when I do it with my enum the result is 'undefined': let six = TokenLength.SIX; console.log(TokenLength[six]); – Anaa Apr 10 '18 at 07:47
  • Remove the quotes around the 6 and 8. – kimy82 Apr 10 '18 at 08:00
0

An answer already exist https://stackoverflow.com/a/18112157/7771064

for (var size in TokenLength) {
    if (!parseInt(size))
        console.log(size);
}

demo: https://stackblitz.com/edit/typescript-buqfne

0

According to the typescript documentation:

The caveat is that string-initialized enums can’t be reverse-mapped to get the original enum member name.

So unfortunately it's not possible.

Anaa
  • 1,191
  • 1
  • 9
  • 21
0
function getTokenLength(value: string): TokenLength | undefined {
    const keys = Object.keys(TokenLength);
    const values = keys.map(k => TokenLength[k as any]);
    const index = values.indexOf(value);
    return index > -1 ? TokenLength[index] as TokenLength: undefined;
}
habsq
  • 1,827
  • 16
  • 17