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'];
}
}