简体   繁体   中英

How to group the json array by specified object value?

I've json right here .
and this is the result in table: 在此处输入图片说明
and this is my javascript to get the json and parse it into table:

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"); 
                }
            });
        }

I want to group the json array by the object name tipe . so the table will be grouped by the tipe and will be like this:
在此处输入图片说明

The question is what should I do with my looping in javascript? Thank you

Create 5 tables with 5 id each equal to your one of 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM