如何按指定的对象值对json数组进行分组?

时间:2012-07-11 06:55:52

标签: javascript arrays json

我是json right here 这是表中的结果: enter image description here
这是我的javascript获取json并将其解析为表:

function detail(kodenegara, koderesult)
        {
            $.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++){
                        details += 
                          "<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>";
                    }
                    $("#table_" + kodenegara)
                    .empty()
                    .append(details)
                    .trigger('create');
                    //show the page
                    $.mobile.changePage("#detail_"+kodenegara, "slide", false, true);
                },
                error: function () { 
                    alert("ERROR"); 
                }
            });
        }

我想按对象名tipe对json数组进行分组。所以表格将按tipe进行分组,如下所示:
enter image description here

问题是我应该如何处理我在javascript中的循环?谢谢

1 个答案:

答案 0 :(得分:1)

创建5个表,其中5个id等于你的tipe。

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="jquery.js"></script>

    <script type="text/javascript">
function detail(kodenegara, koderesult)
        {
            $.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).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>");
                    }
                    $("#table_" + kodenegara)
                    .empty()
                    .append(details)
                    .trigger('create');
                    //show the page
                    $.mobile.changePage("#detail_"+kodenegara, "slide", false, true);
                },
                error: function () { 
                    alert("ERROR"); 
                }
            });
        }

</script>

<style></style><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<table id="ESL" border="1"></table>
<table id="ESL500ML" border="1"></table>
<table id="UHT" border="1"></table>
<table id="WHP" border="1"></table>
<table id="CHEESEIK" border="1"></table>
</body>
</html>