当我有效使用非点符号时 jshint "Better written in dot notation" 的解决方案

Solution to jshint "Better written in dot notation" when i have valid use of non-dot notation

当我得到一个字符串时,如何使用点符号?

我正在编写一些代码来填充 angular 'x-editable' 类型的控件。根据我的 webapi 服务将传回给我的内容,我有一个预定义的值数组,其中包含一个字符串标识符。它发回一个字符串。基于这个字符串,我使用以下方法从我预定义的数组中选择对象:

valuetoshow = myarray['stringFromWebApiCall'];

JSHINT 不合适,因为它要我使用点表示法。我明白为什么 JSHINT 会告诉我这个,我也明白它告诉我的是哪几行,而且我知道如果我将代码更改为 "answers.undergraduate = bigarray" 之类的代码,它会修复 jshint。 当我在下面的代码中提供了一个字符串时,我只是不知道如何使用 .notation 访问数组。

javascript 中是否有某种方法可以让我使用字符串来查找点表示法中的内容?我习惯了 C# 和这种准类型的奇数变量定义,事实证明我很难理解它。

我应该尝试抑制错误吗?我应该在 api 结果上写一个丑陋的 switch 语句吗?

这是真正的代码

    answers['UNDERGRADUATE'] = [
      { 'name': 'Find a job', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Create a network with STTI members', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Receive nursing guidance', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Learn and grow through online continuing nursing education', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false }
    ];

    answers['GRADUATE'] = [
      { 'name': 'Find a job', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Expand your network with STTI members', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Grow your portfolio', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Develop advanced leadership skills', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Stay current on nursing trends', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Learn and grow through online continuing nursing education', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false }
    ];

    answers['NURSE LEADER'] = [
      { 'name': 'Find a job', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Expand your network with STTI members', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Grow your portfolio', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Develop advanced leadership skills', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Stay current on nursing trends', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Learn and grow through online continuing nursing education', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false }
    ];

    answers['HONORARY'] = [
      { 'name': 'Find a job', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Expand your network with STTI members', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Grow your portfolio', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Develop advanced leadership skills', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Stay current on nursing trends', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Learn and grow through online continuing nursing education', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false }
    ];

    answers['DOCTORATE'] = [
      { 'name': 'Find a job', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Expand your network with STTI members', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Grow your portfolio', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Develop advanced leadership skills', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Stay current on nursing trends', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Learn and grow through online continuing nursing education', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false }
    ];

    answers['MASTERS'] = [
      { 'name': 'Find a job', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Expand your network with STTI members', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Grow your portfolio', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Develop advanced leadership skills', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Stay current on nursing trends', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Learn and grow through online continuing nursing education', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false }
    ];

    answers['UNDEFINED'] = [
      { 'name': 'Find a job', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Expand your network with STTI members', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Grow your portfolio', 'ticked': false, 'hideThisGroup':false, 'checkboxDisabled': false },
      { 'name': 'Develop advanced leadership skills', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Stay current on nursing trends', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false },
      { 'name': 'Learn and grow through online continuing nursing education', 'ticked': false, 'hideThisGroup':true, 'checkboxDisabled': false }
    ];

    if ($rootScope.constituent != undefined){
        if ($rootScope.constituent.InductedAs != undefined) {
            $scope.constituentPriorities = answers[$rootScope.constituent.InductedAs.toUpperCase()];
        } else {
            $scope.constituentPriorities = answers['UNDEFINED'];
        }   
    }

Is there a way to use dot notation to accomplish something like this

...是吗?

answers.UNDERGRADUATE = ...

等等

澄清一下:您需要在代码上方编写内容,即数据的实际声明,如answers.UNDERGRADUATE。 JSHint 没有抱怨这一行:

... answers[$rootScope.constituent.InductedAs.toUpperCase()];

显然那一行不能用点符号来写。 JSHint 抱怨的行是字面上写为 answers['UNDEFINED']answers['UNDERGRADUATE'] 的行。这些是您需要修复以使 JSHint 静音的行。