2015-01-16 45 views
5

當我提供字符串時,如何使用點符號?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']; 
     } 
    } 
+1

檢查對象是否有屬性。 http://stackoverflow.com/questions/135448/how-do-i-check-if-an-object-has-a-property-in-javascript – Hobbs

+0

呃...代碼中唯一需要括號表示的屬性名稱是'NURSE LEADER',您可以用點符號來寫所有其他屬性名稱。 – Teemu

+1

你沒有_strings_,你在方括號內有_primitives_。它們是硬編碼的,因此不會動態創建。如果您需要括號內的變量,請刪除引號。只有包含'$,_,AZ,az,0-9'之外的字符的屬性名稱需要括號符號_hardcoded_, – Teemu

回答

11

是否有使用點符號來完成這樣的事情

的方式......是的?

answers.UNDERGRADUATE = ... 

澄清:你需要寫東西上述你的代碼,數據的實際申報,爲answers.UNDERGRADUATE。 JSHint並不抱怨這一行:

... answers[$rootScope.constituent.InductedAs.toUpperCase()]; 

很明顯,該行不能用點符號來書寫。 JSHint抱怨的這些行是字面上寫爲answers['UNDEFINED']answers['UNDERGRADUATE']的行。這些是你需要修復沉默JSHint的行。

+0

我想你誤解了這個問題..我不知道變量名。它的動態。 – CarComp

+1

@CarComp'UNDERGRADUATE','GRADUATE'等從哪裏來的?... – War10ck

+0

我的webapi返回了一個巨大的變量數組,其中包括array.InductedAs ='UNDERGRADUATE'等。 – CarComp

相關問題