Javascript枚举到对应的字符串值

Mat*_*son 26 javascript enums

所以我在我的页面的javascript中有这个:

var TEST_ERROR  = {
        'SUCCESS'   :   0,
        'FAIL'      :   -1,
        'ID_ERROR'  :   -2
      };
Run Code Online (Sandbox Code Playgroud)

并对页面中的函数执行测试,如下所示:

function test()
{
    // Get the paragraph from the DOM
    var testResultParagraph = document.getElementById('testResult');

    // Check the paragraph is valid
    if(!testResultBox)
    {
        // Update the web page
        testResultParagraph.value = TEST_ERROR.ID_ERROR;
        return TEST_ERROR.ID_ERROR;
    }

    // Something to store the results
    var testResult = TEST_ERROR.SUCCESS;

    // Test the calculation
    testResult = testCalculate()

    // Update the web page
    testResultParagraph.value = testResult;

    // The test succeeded
    return TEST_ERROR.SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

testCalculate()段落的结果和值将为0,-1,-2,具体取决于结果.

现在我想将其映射到字符串,以便段落显示"成功","失败"或"ID错误"

我可以通过以下几种方式做到这一点:

var TEST_ERROR  = {
        'SUCCESS'   :   {VALUE : 0 , STRING: 'Success' },
        'FAIL'      :   {VALUE : -1, STRING: 'Fail'    },
        'ID_ERROR'  :   {VALUE : -2, STRING: 'Id Error'},
      };
Run Code Online (Sandbox Code Playgroud)

需要修改枚举点访问器,或

var TEST_ERROR  = {
        'SUCCESS'   :   0,
        'FAIL'      :   1,
        'ID_ERROR'  :   2
      };

var TEST_STRING = [
        'Success',
        'Fail',
        'ID Error'
      ];
Run Code Online (Sandbox Code Playgroud)

这需要改变逻辑(result > TEST_ERROR.SUCCESS看起来很奇怪!)

我的问题是你如何将枚举器值映射到Javascript中的字符串值? 我认为第二种方式是最明智的,但希望调查员对成功是积极的,对失败则是消极的.我也喜欢第一个包含对象结构中的字符串和值的想法.

有任何想法吗?

谢谢!

马特

PS.我将在Web Worker中进行测试,这样页面就不会挂起,结果将被放入表格中,而不是像上面那样的段落.

PPS.我是Javascript编程的新手,但在ASM,C,C++,C#中做了很多.

Vic*_*let 24

不是最优的,但是在没有预先计算反向字典的情况下你可以得到最干净的(此外,如果你只有几个枚举值,这不应该是一个问题):

function string_of_enum(enum,value) 
{
  for (var k in enum) if (enum[k] == value) return k;
  return null;
}
Run Code Online (Sandbox Code Playgroud)

  • 很棒!这应该是公认的答案,适用于任何枚举,没有限制。 (2认同)
  • 很好。我认为** return enum [value]; **也会起作用。 (2认同)

Luk*_*keH 23

你真的需要数值吗?如果没有,那么你可以使用这样的东西:

var TEST_ERROR  = {
    SUCCESS  : 'Success',
    FAIL     : 'Fail',
    ID_ERROR : 'ID Error'
};
Run Code Online (Sandbox Code Playgroud)


小智 9

我更喜欢下面的方法。

enum ColorEnum {
  Red,
  Green,
  Blue
}
console.log(ColorEnum[ColorEnum.Red]);
Run Code Online (Sandbox Code Playgroud)


Zha*_*eng 7

对于 TypeScript,有一个更简单的解决方案(如果你坚持使用 JS,请忽略我的回答):

export enum Direction {
    none,
    left = 1,
    right = 2,
    top = 4,
    bottom = 8
}

export namespace Direction {
    export function toString(dir: Direction): string {
        return Direction[dir];
    }

    export function fromString(dir: string): Direction {
        return (Direction as any)[dir];
    }
}

console.log("Direction.toString(Direction.top) = " + Direction.toString(Direction.top));
// Direction.toString(Direction.top) = top
console.log('Direction.fromString("top") = ' + Direction.fromString("top"));
// Direction.fromString("top") = 4
console.log('Direction.fromString("xxx") = ' + Direction.fromString("unknown"));
// Direction.fromString("xxx") = undefined

Run Code Online (Sandbox Code Playgroud)

因为枚举类型被编译成一个对象(字典)。您不需要循环来查找相应的值。

enum Direction {
    left,
    right
}
Run Code Online (Sandbox Code Playgroud)

编译成:

{
    left: 0
    right: 1
    0: "left",
    1: "right"
}
Run Code Online (Sandbox Code Playgroud)

  • 这就是 TypeScript。这是用 JavaScript 标记的。 (2认同)
  • 这实际上对我的 JS 很有帮助。不知道你可以使用整数作为对象键。现在我可以使用“Object.freeze({...});”中的“enum”对象来映射两个方向 (2认同)