如何按指定的对象值对json数组进行分组?
问题描述:
我有json right here。
这就是结果表:
,这是我的javascript来获得JSON并将其解析为表:
如何按指定的对象值对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
进行分组,将是这样的:
的问题是,我该怎么办我在javascript循环?谢谢
答
创建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>
现在它工作:))但如果表的数量是动态的,并获得表的数量是通过对'tipe'进行分组?因为没有包含5'tipe'的结果(可以是3或4)。 :))非常感谢你:D – blankon91 2012-07-11 08:08:52