2015-01-16 4 views
5

문자열을 제공 할 때 도트 표기법을 사용하는 방법은 무엇입니까?jshint 솔루션 "도트 표기법으로 작성하는 것이 더 낫습니다."도트가 아닌 표기법을 사용할 때

각형의 'x 편집 가능한'컨트롤 유형을 채우기 위해 일부 코드를 작성했습니다. 내 webapi 서비스가 나에게 전달할 문자열 식별자를 기반으로 사전 정의 된 값 배열을 가지고 있습니다. 문자열을 돌려 보냅니다. 그것은 나를 점 표기법을 사용하고 싶어하기 때문에

valuetoshow = myarray['stringFromWebApiCall']; 

JSHINT는 적합을 던지고있다 :이 문자열을 기반으로, 나는 다음과 같은 방법을 사용하여 미리 정의 된 배열에서 객체를 선택합니다. JSHINT가 나에게 말하는 이유를 이해하고 어떤 줄을 쓰는지 이해한다. 코드를 "answers.undergraduate = bigarray"와 같이 변경하면 jshint가 수정 될 것이다. 아래 코드에서 문자열을 제공 할 때 .notation을 사용하여 배열에 액세스하는 방법을 알지 못합니다.

문자열을 사용하여 점 표기법을 찾는 자바 스크립트에서 일종의 메소드가 있습니까? 나는 C#에 익숙하다.이 준 타입의 변수를 정의하는 것은 내 머리를 감싸는 것이 힘들다는 것을 증명한다.

  • ['UNDERGRADUATE'] is better written in dot notation.
  • 내가 오류를 억제하려고 시도해야
  • ['MASTERS'] is better written in dot notation.
  • ['UNDEFINED'] 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.
  • ? 나는 단지 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

    괄호 안에 _stimings_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