2012-04-20 3 views
0

배열을 푸시 할 수있는 기능이 있지만 문제가 발생하는 이유는 항상 조건이 함께 점검되기 때문입니다. 이걸 제외하고, 곱슬 머리를 사용하면 배열 번호가 생성됩니다. 시대의. 심지어 내가 제대로 여기에 설명 할 수없는, 내 코드를 봐 :jquery가 조건부 일 경우

$.map(pObj,function(values,i){ 
    if(typeof pieArray[values.GeoID] == 'undefined') 
     pieArray[values.GeoID] = []; //it should create only one time 

    pieArray[values.GeoID].push(values.ResponsePercentage); // when it matches it should not go down to check next if. else it can go. 

    if(!values.GeoID && typeof pieArray[0] == 'undefined') 
     pieArray[0] = []; //it should create only one time 

    pieArray[0].push(values.ResponsePercentage); //now the top if get response, till it is checking and throwing error. top if not match, then it need to work. 
}); 

나는 이것을 어떻게 할 수 있습니까?

+2

제발 모욕으로 생각하지 마십시오. 영어 원어민이 아니며 형식이 잘못된 질문과 문법 사이에서 당신이 말하는 것을 이해하려고 힘듭니다. 다시 설명하거나 더 많은 코드를 제공 할 수 있습니까? – RASG

+0

죄송합니다. 몇 가지 해결 방법이 있는데 문제가 해결되었습니다. 감사. – 3gwebtrain

답변

0

나는 이렇게했습니다. 그것은 잘 작동합니다.

var pieArray = []; 
var allIndia = false; //using as a key. 

$.ajax({ 
     cache: false, 
     type: "GET", 
     async: false, 
     url: url, 
     dataType: "jsonp", 
     success: function (pObj) { 
     $.map(pObj,function(values,i){ 
      if(typeof pieArray[values.GeoID] == 'undefined') pieArray[values.GeoID] = [],allIndia = false;//making false 
      pieArray[values.GeoID].push(values.ResponsePercentage); 

      if(!values.GeoID && typeof pieArray[0] == 'undefined') pieArray[0] = [], allIndia = true;//making true, so it will not go down the first condition matches. 
      if(allIndia) {pieArray[0].push(values.ResponsePercentage)}; 
     }) 
     pieArray = $.grep(pieArray,function(n){ 
      return(n); 
     }); 
     console.log(pieArray); 
     makePieChart(); 
     }, 
     error: function (err) { 
     //console.log(err); 
     } 
    }); 

감사합니다.

0

질문을 올바르게 이해하면 제어 블록 ({}) 만 사용해야합니다.

$.map(pObj,function(values,i) { 
     if(typeof pieArray[values.GeoID] == 'undefined') { 
      //it should create only one time 
      pieArray[values.GeoID] = []; 
      // when it matches it should not go down to check next if. else it can go. 
      pieArray[values.GeoID].push(values.ResponsePercentage); 
     } 
     if(!values.GeoID && typeof pieArray[0] == 'undefined') { 
      //it should create only one time 
      pieArray[0] = []; 
      //now the top if get response, till it is checking and throwing error. top if not match, then it need to work. 
      pieArray[0].push(values.ResponsePercentage); 
     } 
    }); 
+0

그게 내가하는 말, 내가 이것을 좋아할 때, 'pieArray [values.GeoID] = []; '은 값에 따라 no.of 시간을 만듭니다. – 3gwebtrain

관련 문제