6

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'];
        }   
    }
Rounin
  • 27,134
  • 9
  • 83
  • 108
CarComp
  • 1,929
  • 1
  • 21
  • 47
  • 1
    Check if the object has the property. http://stackoverflow.com/questions/135448/how-do-i-check-if-an-object-has-a-property-in-javascript – Hobbes Jan 16 '15 at 15:31
  • Umh... The only property name in the code requiring bracket notation is `NURSE LEADER`, you can write all others with the dot notation. – Teemu Jan 16 '15 at 16:15
  • 1
    You don't have _strings_, you've _primitives_ within the brackets. They are hardcoded, hence not dynamically created. If you need a variable within the brackets, remove the quotes. Only property names containing chracters outside of `$, _, A-Z, a-z, 0-9` need bracket notation when _hardcoded_, – Teemu Jan 16 '15 at 17:03
  • http://stackoverflow.com/questions/13192466/how-to-suppress-variable-is-better-written-in-dot-notation – Christophe Roussy Jan 11 '17 at 10:57

1 Answers1

13

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.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • I think you misread the question.. I don't know the variable name. Its dynamic. – CarComp Jan 16 '15 at 15:28
  • My webapi returns a huge variable array including where array.InductedAs = 'UNDERGRADUATE' etc. – CarComp Jan 16 '15 at 15:30
  • @CarCamp UM, the error would not occur with `answers[$r....]`, the error is with where you are defining it like this answer states. – epascarello Jan 16 '15 at 15:30
  • 1
    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. – user229044 Jan 16 '15 at 15:31
  • 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? – CarComp Jan 16 '15 at 15:34
  • 2
    @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]`. – user229044 Jan 16 '15 at 15:36