js 根据数组分组动态生成table(相同项合并)

  
<html>  
 <head>  
  <title> New Document title>  
  <script>  
  //xxbh:学校编号,bjbh:班级编号,xx:姓名  
    var arrJson=[  
        {'xxbh':'1001','bjbh':'0801','xx':'李四'},{'xxbh':'1001','bjbh':'0802','xx':'张三'},  
        {'xxbh':'1003','bjbh':'0803','xx':'王五'},{'xxbh':'1003','bjbh':'0804','xx':'晶晶'},  
        {'xxbh':'1005','bjbh':'0701','xx':'亮亮'},{'xxbh':'1002','bjbh':'0701','xx':'明敏'},  
        {'xxbh':'1005','bjbh':'0701','xx':'张灿'},{'xxbh':'1002','bjbh':'0801','xx':'小白'},  
        {'xxbh':'1005','bjbh':'0802','xx':'小胖'},{'xxbh':'1002','bjbh':'0901','xx':'小丽'},  
        {'xxbh':'1003','bjbh':'0901','xx':'阿宝'},{'xxbh':'1004','bjbh':'0901','xx':'王泰'},  
        {'xxbh':'1001','bjbh':'0901','xx':'阿毛'},{'xxbh':'1006','bjbh':'0901','xx':'李铭'},  
        {'xxbh':'1005','bjbh':'0901','xx':'阿城'},{'xxbh':'1004','bjbh':'0901','xx':'张晓'},  
        {'xxbh':'1006','bjbh':'0901','xx':'小小'},{'xxbh':'1004','bjbh':'0901','xx':'白昼'},  
        {'xxbh':'1002','bjbh':'0901','xx':'乐乐'},{'xxbh':'1001','bjbh':'0901','xx':'黎明'}  
    ];  
    function creatMapByXxbh(arrJson){  
        var xxbhMap={};  
        var len = arrJson.length;  
        for(var i=0;iif(xxbhMap[arrJson[i].xxbh] == undefined){  
                var list = [];  
                list.push(arrJson[i]);  
                xxbhMap[arrJson[i].xxbh] = list;  
            }else{  
                xxbhMap[arrJson[i].xxbh].push(arrJson[i]);  
            }  

        }  
        var str="";  
        for(var xxbh in xxbhMap){  
            var stu = xxbhMap[xxbh];  
            alert(stu.length);  
            str+="";  
            for(var i = 0;i < stu.length;i++){  
                //document.write(xxbh + " "+stu[i].bjbh+" "+stu[i].xx +"
")
//document.write("
学校编号班级姓名
"+xxbh+"
学校编号班级姓名
")
if(i==0){ str+=""+stu[i].bjbh+""+stu[i].xx+""; }else{ str+=""+stu[i].bjbh+""+stu[i].xx+""; } } str+=""+xxbh+"共有"+stu.length+"个班级" } str+=" "; document.write(str) } script> head > <body> <button onclick='creatMapByXxbh(arrJson)'>测试button> body> html>

你可能感兴趣的:(JavaScript)