Javascript Enum To Corresponding String Value

前端 未结 8 1990
攒了一身酷
攒了一身酷 2021-01-07 17:06

So I have this in the javascript for my page:

var TEST_ERROR  = {
        \'SUCCESS\'   :   0,
        \'FAIL\'      :   -1,
        \'ID_ERROR\'  :   -2
            


        
8条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-07 17:32

    Building on Victor's excellent answer, so it works in TypeScript:

    enumToStr(enumeration: any, value: any): string {
      for (var k in enumeration)
        if (enumeration[k] == value)
          return k;
      return null;
    }
    

提交回复
热议问题