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

15,102

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 above your code, the actual declaration of data, as answers.UNDERGRADUATE. JSHint is not complaining about this line:

... 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.

Share:
15,102
CarComp
Author by

CarComp

I write code. Sometimes good code. Sometimes bad. But it works.

Updated on June 15, 2022

Comments

  • CarComp
    CarComp almost 2 years

    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:

    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.

    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.

    • ['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?

    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'];
            }   
        }
    
  • CarComp
    CarComp over 9 years
    I think you misread the question.. I don't know the variable name. Its dynamic.
  • CarComp
    CarComp over 9 years
    My webapi returns a huge variable array including where array.InductedAs = 'UNDERGRADUATE' etc.
  • epascarello
    epascarello over 9 years
    @CarCamp UM, the error would not occur with answers[$r....], the error is with where you are defining it like this answer states.
  • user229044
    user229044 over 9 years
    Why are you running the result from an API through JSHint? This question makes no sense. The errors you posted indicate JSHint is taking exception to the literal typed out string answers['UNDERGRADUATE'], not some other string answers[variableName]. You shouldn't be running your API response through JSHint. You also shouldn't be returning JavaScript. This is what JSON is for.
  • CarComp
    CarComp over 9 years
    i'm not. i think everyone here is misunderstanding what i'm asking. If I have a string, and ONLY a string variable, how can i use it to look up what is in an array without using the old varablename['stringname'] method?
  • user229044
    user229044 over 9 years
    @CarComp You can't, and JSHint isn't asking you to. One more time, and then I'm not replying again because you're not listening to me: The hints you're getting are about the literal sequence of characters answers['UNDERGRADUATE']. That is all. You need to rewrite that specific series of characters to read answers.UNDEFINED to silence JSHint. Those specific characters are present in the code you're running through JSHint, or JSHint wouldn't be giving you that hint. Fix those. JSHint cannot provide such a hint for something like answer[variableName].