0

我有一个推送数组的功能,但我遇到了问题,因为我的条件总是一起检查。除此之外,如果我使用花括号,我将创建数组编号。次。即使我无法在这里正确解释,请查看我的代码:

$.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.
});

我怎样才能做到这一点?

4

2 回答 2

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);
       }
    });
于 2012-04-20T12:27:55.577 回答
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);
        }
    });

谢谢大家。

于 2012-04-20T12:49:02.137 回答