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'];
}
}
However, the dot notation is often preferred because it is easier to read, less verbose, and works better with aggressive JavaScript minimizers.
Dot notation is one way to access a property of an object. To use dot notation, write the name of the object, followed by a dot (.), followed by the name of the property. Example: var cat = { name: 'Moo', age: 5, }; console.
Dot notation allows us to tell a instance of a class to use one of the methods inside that class. So if we called michael_rochlin. say_name() it would call the method say_name() that was written in the person class because michael_rochlin is a person .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With