2012-04-20 72 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 2012-04-20 12:27:32

+0

對不起。我有一些解決方法,並修復了問題。謝謝。 – 3gwebtrain 2012-04-20 12:50:51

回答

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] = [];根據價值觀,' '正在創造次數。 – 3gwebtrain 2012-04-20 12:37:35