2013-10-20 2 views
1

아래의 JSON 응답이 있습니다. $ .getJSON 메서드를 사용하여 JSON 데이터를로드하고 콜백 함수를 사용하여 아래와 같은 배열인지 확인하여 일부 조작을 수행합니다.자바 스크립트 배열 객체와 관련된 문제

{ 
    "r": [{ 
     "IsDefault": false, 
     "re": { 
      "Name": "Depo"    
     }, 
     "Valid": "Oct8, 2013", 
     "Clg": [{ 
      "Name": "james", 
      "Rate": 0.05 
     }, { 
      "Name": "Jack", 
      "Rate": 0.55 
     }, { 
      "Name": "Mcd", 
      "Rate": 0.01, 
     }], 
    }, 
    { 
     "IsDefault": false, 
     "re": { 
      "Name": "Depo" 
     }, 
     "Valid": "Oct8, 2013", 
     "Clg": [{ 
      "Name": "james", 
      "Rate": 0.05 
     }, { 
      "Name": "Jack", 
      "Rate": 0.55 
     }, { 
      "Name": "Mcd", 
      "Rate": 0.01, 
     }], 
    }, 
    { 
     "IsDefault": false, 
     "re": { 
      "Name": "Depo" 
     }, 
     "Valid": "Oct8, 2013", 
     "Clg": [{ 
      "Name": "james", 
      "Rate": 0.05 
     }, { 
      "Name": "Jack", 
      "Rate": 0.55 
     }, { 
      "Name": "Mcd", 
      "Rate": 0.01, 
     }], 
    }] 
} 

아래와 같이 loadFromJson1 및 loadFromJson2 함수에서 json 응답을 "input"매개 변수로 전달합니다.

var tablesResult = loadFromJson1(resultstest.r[0].Clg); 
    loadFromJson1 = function (input) { 
     if (_.isArray(input)) { 
     alert("loadFromJson1: Inside array function"); 
      var collection = new CompeCollection(); 
      _.each(input, function (modelData) { 
       collection.add(loadFromJson1(modelData)); 
      }); 
      return collection; 
     } 
     return new CompeModel({ 
      compeRates: loadFromJson2(input), 
      compName: input.Name 
     }); 
    }; 

    loadFromJson2 = function (input) 
    // here is the problem, the 'input' is not an array object so it is not going to IF condition of the isArray method. 
    { 
     if (_.isArray(input)) { 
      alert("loadFromJson2: Inside array function"); 
      //alert is not coming here though it is an array 
      var rcollect = new rateCollection(); 
      _.each(input, function (modelData) { 
       rcollect.add(modelData); 
      }); 
      return rcollect; 
     } 
    }; 

위 코드는 "input"으로 loadFromJson1 및 loadFromJson2 함수에 대한 json 응답을 전달합니다. isArray는 loadFromJson1 함수에서만 true가되고 if 조건 내에서 경고하지만 loadFromJson2 함수에서는 오지는 않지만 동일한 매개 변수를 전달합니다.

누군가가 배열 객체를 전달하더라도 loadFromJson2 함수가 경고를받지 못하는 이유를 말해 줄 수 있습니까?

+0

여분의 쉼표가있는 것 같습니다. – ajax333221

답변

1

input이 배열 인 경우 loadFromJson2을 호출하지 마십시오. input이 아니고 배열이 인 경우에만 호출합니다. 따라서 안에 _.isArray(input)은 절대 적용되지 않습니다.

Here's a jsfiddle (로그를 보려면 브라우저의 자바 스크립트 콘솔을 켜십시오). 당신은 당신의 입력에서이 출력을 얻을 :

into loadFromJson1 call #1 (index):82 
loadFromJson1 #1: it's an Array (index):84 
loadFromJson1 #1: Inside array function (index):85 
into loadFromJson1 call #2 (index):82 
loadFromJson1 #2: not an array (index):93 
loadFromJson1 #2: calling loadFromJson2 and returning (index):95 
into loadFromJson2 (index):105 
loadFromJson2: not an array (index):115 
into loadFromJson1 call #3 (index):82 
loadFromJson1 #3: not an array (index):93 
loadFromJson1 #3: calling loadFromJson2 and returning (index):95 
into loadFromJson2 (index):105 
loadFromJson2: not an array (index):115 
into loadFromJson1 call #4 (index):82 
loadFromJson1 #4: not an array (index):93 
loadFromJson1 #4: calling loadFromJson2 and returning (index):95 
into loadFromJson2 (index):105 
loadFromJson2: not an array (index):115 
loadFromJson1 #1: returning (index):90 

당신이 볼 수 있듯이, loadFromJson2에 대한 호출이 loadFromJson1중첩 통화에서입니다 - 하지 배열 무언가를 얻을 것들.

+0

안녕하세요, console.log ("return statement"후)를 추가했습니다. return 문 다음에 return 문 다음에 코딩이 실행 중임을 알 수 있습니다. 왜 입력이 배열이 아니라 배열이라고 말하고 있는지 말해 주시겠습니까? – ezhil

+0

'input'이 배열 인 경우,'load'를 포함하여 처음'if'의 본문이 실행되며,'loadFromJson1'을 완전히 종료합니다. 'return'을 지나치는 유일한 방법은 배열이 아닌'loadFromJson1'에 무엇인가를 전달하는 것입니다. 어떤 경우에는 'loadFromJson2'를 호출 할 것이지만'loadFromJson2'에 전달하는 것은 배열이 아니기 때문에 (로드 밸런싱는'loadFromJson1'에서 결코 얻을 수 없었을 것입니다) , 그것은 여전히 ​​"loadFromJson2"의 "if it 's array"블록에 들어 가지 않습니다. –

+0

그러나 위의 json 응답 에서처럼 입력은 절대적으로 배열입니다. 따라서 첫 번째 if 조건은 항상 true가되고 if 조건을 완료하고 컬렉션을 반환합니다. 그런 다음 그것은 다음 매개 변수에 전달됩니다. 동일한 매개 변수를 전달하지만 사실이 아닙니다. 이유는 알지 못합니다. return 문 다음에 아무 것도하지 않으면 Java에서 오류가 발생합니다. – ezhil