Jshint's “Better written in dot notation” solution when I have actual use of non-dot notation

How to use dot notation when a string is provided to me?

I am writing code to populate the angular x-editable control type. I have an array of values ​​predefined by a string identifier based on my webapi service coming back to me. It sends back the string. Based on this line, I select an object from the array that I previously defined using the following method:

valuetoshow = myarray['stringFromWebApiCall']; 

JSHINT throws a fit because it wants me to use dot notation. I understand WHY JSHINT tells me this, and also I understand what lines this tells me, and I know that if I change my code to something like "answer.undergraduate = bigarray", it will fix jshint. I just don't know what to do with array access using .notation when I get a line in the code below.

Is there any method in javascript that allows me to use a string to search for something in dotted notation? I'm used to C # and this is a quasi-typed weird variable definition that is hard for me to wrap around.

  • ['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 try to suppress the error? Should I just write a big ugly switch statement on api results?

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']; } } 
+5
source share
1 answer

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

... Yes?

 answers.UNDERGRADUATE = ... 

etc.

To clarify: you need to write your code above , the actual data declaration, as answers.UNDERGRADUATE . JSHint does not complain about this line:

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

Obviously, this line cannot be written using dot notation. The lines that JSHint complains are lines that are literally written out as answers['UNDEFINED'] or answers['UNDERGRADUATE'] . These are the lines you need to fix to disable JSHint.

+11
source

Source: https://habr.com/ru/post/1211265/


All Articles