gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 05:02:22 25 4
gpt4 key购买 nike

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

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

valuetoshow = myarray['stringFromWebApiCall'];

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

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

  • ['UNDERGRADUATE'] 最好用点号书写。
  • ['GRADUATE'] 最好用点号书写。
  • ['HONORARY'] 最好用点号书写。
  • ['DOCTORATE'] 最好用点号书写。
  • ['MASTERS'] 最好用点号书写。
  • ['UNDEFINED'] 最好用点号书写。

我应该尝试抑制错误吗?我应该只在 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 静音的行。

关于javascript - 当我有效使用非点符号时 jshint "Better written in dot notation"的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27987218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com