js数组按日期分类。

高冷boy 发布于 2017/01/06 20:03
阅读 536
收藏 0
JS

数组如下


var arr=[
    { "name": "张奇", "Time": "2017-01-03 19:36:33", "charge": "800", "totaltime": "7599000", "reachTime": "2017-01-04"  },
    { "name": "张奇", "Time": "2017-01-03 17:22:22", "charge": "100", "totaltime": "922635", "reachTime": "2017-01-03"  },
    { "name": "张奇", "Time": "2017-01-02 19:36:33", "charge": "200", "totaltime": "", "reachTime": "2017-01-02"  },
    { "name": "张奇", "Time": "2017-01-01 16:20:07", "charge": "100", "totaltime": "1457076", "reachTime": "2017-01-01"  }
]

期望得到

var tests=[
    {time: "2017-01-03",money:'900', file:[
            {Time: "2017-01-03", charge: "800", name: "张奇", reachTime: "2017-01-04 09:30:00", totaltime: "7599000"},
             {Time: "2017-01-03", charge: "100", name: "张奇", reachTime: "2017-01-02 12:00:00", totaltime: "1457076"}
    ]},
    {time: "2017-01-02",money:'1000', file:[
        {Time: "2017-01-02", charge: "800", name: "张奇", reachTime: "2017-01-04 09:30:00", totaltime: "7599000"}
    ]},
    {time: "2017-01-01",money:'800', file:[
            {Time: "2017-01-01", charge: "800", name: "张奇", reachTime: "2017-01-04 09:30:00", totaltime: "7599000"}
    ]},

]

加载中
0
叶飞飘渺
叶飞飘渺
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>数组归类</title>
</head>
<body>
<div id="print">

</div>
<script type="application/javascript">

    var arr=[
        { "name": "张奇", "Time": "2017-01-03 19:36:33", "charge": "800", "totaltime": "7599000", "reachTime": "2017-01-04"  },
        { "name": "张奇", "Time": "2017-01-03 17:22:22", "charge": "100", "totaltime": "922635", "reachTime": "2017-01-03"  },
        { "name": "张奇", "Time": "2017-01-02 19:36:33", "charge": "200", "totaltime": "", "reachTime": "2017-01-02"  },
        { "name": "张奇", "Time": "2017-01-01 16:20:07", "charge": "100", "totaltime": "1457076", "reachTime": "2017-01-01"  }
    ];
    var map = new Object();
    for(var i = 0; i< arr.length;i++){
        var item = arr[i];
        var time = item.Time.split(' ')[0];
        if(!map[time]){
            var array = new Array();
            array.push(item);
            map[time] = {time:time,money: parseInt(item.charge),file:array};
        }else{
            var  temp = map[time];
            temp.money  += parseInt(item.charge);
            temp.file.push(item);
            map[time] = temp;
        }
    }

    var resultArray = new Array();
    for(var key in map){
        resultArray.push(map[key]);
    }
    document.getElementById('print').innerHTML = JSON.stringify(resultArray);


</script>
</body>

</html>

结果:

[{"time":"2017-01-03","money":900,"file":[{"name":"张奇","Time":"2017-01-03 19:36:33","charge":"800","totaltime":"7599000","reachTime":"2017-01-04"},{"name":"张奇","Time":"2017-01-03 17:22:22","charge":"100","totaltime":"922635","reachTime":"2017-01-03"}]},{"time":"2017-01-02","money":200,"file":[{"name":"张奇","Time":"2017-01-02 19:36:33","charge":"200","totaltime":"","reachTime":"2017-01-02"}]},{"time":"2017-01-01","money":100,"file":[{"name":"张奇","Time":"2017-01-01 16:20:07","charge":"100","totaltime":"1457076","reachTime":"2017-01-01"}]}]

高冷boy
谢谢,你是个好人。
0
C
CallMain
上面变成下面的数据都没有规律别人怎么帮你?
返回顶部
顶部