當我提供字符串時,如何使用點符號?jshint的解決方案當我有效使用非圓點符號時,「用點表示法寫得更好」
我正在寫一些代碼來填充角度'x-editable'類型的控件。我有一個基於我的webapi服務將返回給我的字符串標識符預定義的值數組。它發回一個字符串。在此基礎上串,我選擇從陣列中使用下面的方法我已經預先定義的對象:因爲它要我用點號
valuetoshow = myarray['stringFromWebApiCall'];
JSHINT拋出一個合適的。我明白爲什麼JSHINT告訴我這一點,並且我明白它告訴我的是哪一行,並且我知道如果我將代碼更改爲諸如「answers.undergraduate = bigarray」之類的代碼,它將修復jshint。 我只是不知道如何使用.notation訪問數組,當我在下面的代碼中提供了一個字符串。
在JavaScript中有某種方法可以讓我用字符串來查找點符號中的某些東西嗎?我習慣了C#,而這種類型奇怪的變量定義對我來說很難理解。
['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.
我應該嘗試取消錯誤?我應該在api結果上寫一個大的醜陋的switch語句嗎?
這裏是真正的代碼
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'];
}
}
檢查對象是否有屬性。 http://stackoverflow.com/questions/135448/how-do-i-check-if-an-object-has-a-property-in-javascript – Hobbs
呃...代碼中唯一需要括號表示的屬性名稱是'NURSE LEADER',您可以用點符號來寫所有其他屬性名稱。 – Teemu
你沒有_strings_,你在方括號內有_primitives_。它們是硬編碼的,因此不會動態創建。如果您需要括號內的變量,請刪除引號。只有包含'$,_,AZ,az,0-9'之外的字符的屬性名稱需要括號符號_hardcoded_, – Teemu