Reverse-Mapping for String Enums

后端 未结 4 684
星月不相逢
星月不相逢 2020-12-05 23:12

I wanted to use string enums in typescript but I can\'t see a support for reversed mapping in it. I have an enum like this:

enum Mode {
    Silent = \"Silent         


        
4条回答
  •  情歌与酒
    2020-12-05 23:55

    None of the answers really worked for me, so I have to fall back to normal for the loop. my enum is

    enum SOME_CONST{
          STRING1='value1', 
          STRING2 ='value2',
          .....and so on
    }
    
    
    getKeyFromValue =(value:string)=> Object.entries(SOME_CONST).filter((item)=>item[1]===value)[0][0];
    

提交回复
热议问题