javascript 当我有效使用非点表示法时,jshint“更好地用点表示法编写”的解决方案

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27987218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:15:42  来源:igfitidea点击:

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

javascriptarrayssyntaxjshint

提问by CarComp

How does one use dot notation when i'm provided a string?

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

I am writing some code to populate an angular 'x-editable' type of control. I have an array of values predefined with a string identifier based on what my webapi service will pass back to me. It sends back a string. Based on this string, i choose the object from the array i have pre-defined using the following method:

我正在编写一些代码来填充有角度的“x 可编辑”类型的控件。我有一个基于我的 webapi 服务将传回给我的内容的字符串标识符预定义的值数组。它发回一个字符串。基于此字符串,我从使用以下方法预定义的数组中选择对象:

valuetoshow = myarray['stringFromWebApiCall'];

JSHINT is throwing a fit because it wants me to use dot notation. I understand WHY JSHINT is telling me this, and also I understand which lines it is telling me about, and I know if I change my code to something like "answers.undergraduate = bigarray" it will fix the jshint. I just don't know what to do about accessing the array using .notation when i'm provided a string in the code below.

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

Is there some sort of method in javascript that lets me use a string to look up something in dot notation? I'm used to C# and this quasi-typed odd defining of variables it proving tricky for me to wrap my head around.

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

  • ['UNDERGRADUATE'] is better written in dot notation.
  • ['GRADUATE'] is better written in dot notation.
  • ['HONORARY'] is better written in dot notation.
  • ['DOCTORATE'] is better written in dot notation.
  • ['MASTERS'] is better written in dot notation.
  • ['UNDEFINED'] is better written in dot notation.
  • ['UNDERGRADUATE'] is better written in dot notation.
  • ['GRADUATE'] is better written in dot notation.
  • ['HONORARY'] is better written in dot notation.
  • ['DOCTORATE'] is better written in dot notation.
  • ['MASTERS'] is better written in dot notation.
  • ['UNDEFINED'] is better written in dot notation.

Should i attempt to suppress the error? Should I just write a big ugly switch statement on the api results?

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

Here is the real code

这是真正的代码

    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'];
        }   
    }

回答by meagar

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

有没有办法使用点符号来完成这样的事情

... Yes?

... 是的?

answers.UNDERGRADUATE = ...

etc

等等

To clarify: You need to write the stuff aboveyour code, the actual declaration of data, as answers.UNDERGRADUATE. JSHint is not complaining about this line:

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

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

Obviously that line cannot be written using dot-notation. The lines that JSHint is complaining about are the lines that are literally written out as answers['UNDEFINED']or answers['UNDERGRADUATE']. Those are the lines you need to fix to silence JSHint.

显然,该行不能使用点表示法编写。JSHint 抱怨的行是字面上写为answers['UNDEFINED']or 的行answers['UNDERGRADUATE']。这些是您需要修复以使 JSHint 静音的行。