0

我有这样的json。在那个 json 中有对象名称tipe,我想在同一个提示中求和act_qty1, act_val1, acvqty, acvval, budqty, budval, cm_val1, (例如cm_val2cm_val3 在它们tipe是“ESL”的地方求和)。如何在javascript中总结?可以用循环完成吗?

这是我到目前为止所做的:

function detail(kodenegara, koderesult)
        {   
            $.mobile.showPageLoadingMsg(); 
            $.ajax({
                type: "GET",
                contentType: "application/json; charset=utf-8",
                url: "http://www.greenfields.co.id:502/Service1.svc/"+kodenegara,
                dataType: "json",
                success:function(data){
                    var result = koderesult;

                    var details = "";

                    for (i = 0; i < data[result].length; i++){
                        $("#"+data[result][i].tipe).empty();
                    }
                    for (i = 0, types={} ; i < data[result].length; i++){
                        $("#"+data[result][i].tipe).append("<tr>"+
                            "<td>"+data[result][i].mc+"</td>"+
                            "<td>"+data[result][i].value3+"</td>"+
                            "<td>"+data[result][i].value2+"</td>"+
                            "<td>"+data[result][i].value1+"</td>"+
                            "<td>"+data[result][i].avgqty+"</td>"+
                            "<td>"+data[result][i].budqty+"</td>"+
                            "<td>"+data[result][i].budval+"</td>"+
                            "<td>"+data[result][i].acvqty+"</td>"+
                            "<td>"+data[result][i].acvval+"</td>"+
                          "</tr>").trigger('create');


                            //----------------------------------//
                            // HERE IS what I've made TO SUM THE VALUES //
                            //vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv//

                           for(i = 0; i < data[result].length; i++) {
                    if(data[result][i].tipe == 'ESL') 
                    {
                        //how to summing data[result][i].cm_val3 where data[result][i].tipe == 'ESL'
                                        var b = i + 1;
                            var test = parseInt(data[result][i].cm_val3) + parseInt(data[result][b].cm_val3)
                    }
                }

                    }

                    //show the page
                    $.mobile.changePage("#detail_"+kodenegara, "slide", false, true);
                },
                error: function () { 
                    alert("ERROR"); 
                }
            });
        }

我不知道如何编写正确的循环来对tipe“ESL”(或“ESL1L”或“WHP”或其他)的值求和。如果我使用这个:

var b = i + 1;
var test = parseInt(data[result][i].cm_val3) + parseInt(data[result][b].cm_val3)

仅对最后一个数组求和。如何编写正确的循环以对该条件求和?

4

2 回答 2

1

这里有一个工作小提琴 http://jsfiddle.net/xKJn8/1/

var SumByTipe = {};
for(i in data.GetReportIdResult){
    var currtipe = data.GetReportIdResult[i].tipe;
    if (currtipe){
        if (currtipe in SumByTipe){
            for (j in data.GetReportIdResult[i]){
                if (j != "tipe" && j != "mc"){
                    SumByTipe[currtipe][j + '_total'] += parseFloat(data.GetReportIdResult[i][j]);
                }
            }
        }else{
            var firstSum = {};
            for (j in data.GetReportIdResult[i]){
                if (j != "tipe" && j != "mc"){
                    firstSum[j + '_total'] = parseFloat(data.GetReportIdResult[i][j]);
                }
            }
            SumByTipe[currtipe]=firstSum;
        }
    }
}
console.debug(SumByTipe);
于 2012-07-13T09:08:45.470 回答
1

这应该够了吧 :

var sum_cm_val3=0;

for (var i=0;i<data[result].length;i++) {
    if (data[result][i].tipe == "ESL") {
        var cm_val3 = data[result][i].cm_val3;
        if (parseInt(cm_val3)==cm_val3) {
            sum_cm_val3 += parseInt(cm_val3);
        }
    }
}
于 2012-07-13T09:28:23.283 回答