2012-04-20 11 views
0

私は配列をプッシュする機能を持っていますが、問題が発生しています。これとは別に、もし私がカーリーブラゼを使用していたら、私は配列番号を作成しています。回。私もここで正しく説明することができません、私のコードを見てください:jquery if condition issue

$.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