当前位置: 动力学知识库 > 问答 > 编程问答 >

group by - how to groupBy dates of JSON data with help of Underscore.js library?

问题描述:

I have following JSON data:

var data = [

{ "allocated":"20", "unallocated":"2", "day":"01/01/2014" },

{ "allocated":"12", "unallocated":"0", "day":"02/01/2014" },

{ "allocated":"2", "unallocated":"18", "day":"03/01/2014" },

{ "allocated":"22", "unallocated":"2", "day":"04/01/2014" },

{ "allocated":"3", "unallocated":"13", "day":"05/01/2014" },

{ "allocated":"6", "unallocated":"2.4", "day":"06/01/2014" },

{ "allocated":"14", "unallocated":"7", "day":"07/01/2014" },

{ "allocated":"14", "unallocated":"7", "day":"08/01/2014" },

{ "allocated":"14", "unallocated":"7", "day":"09/01/2014" },

{ "allocated":"14", "unallocated":"7", "day":"10/01/2014" },

{ "allocated":"14", "unallocated":"7", "day":"11/01/2014" },

{ "allocated":"14", "unallocated":"7", "day":"12/01/2014" },

{ "allocated":"14", "unallocated":"7", "day":"13/01/2014" },

{ "allocated":"14", "unallocated":"7", "day":"14/01/2014" },

{ "allocated":"14", "unallocated":"7", "day":"15/01/2014" },

{ "allocated":"14", "unallocated":"7", "day":"16/01/2014" },

{ "allocated":"14", "unallocated":"7", "day":"17/01/2014" },

{ "allocated":"14", "unallocated":"7", "day":"18/01/2014" },

{ "allocated":"14", "unallocated":"7", "day":"19/01/2014" },

{ "allocated":"14", "unallocated":"7", "day":"20/01/2014" },

{ "allocated":"14", "unallocated":"7", "day":"21/01/2014" },

{ "allocated":"14", "unallocated":"7", "day":"22/01/2014" },

{ "allocated":"14", "unallocated":"7", "day":"23/01/2014" },

{ "allocated":"14", "unallocated":"7", "day":"24/01/2014" },

{ "allocated":"14", "unallocated":"7", "day":"25/01/2014" }

]

Now i want to do GroupBy "WEEK" of this data. So, i need above data in forms of week. How it can be possible by using underscore.js library?

Thanks in advance

网友答案:

Using moment.js and underscore's _.groupBy, you could do the following:

var data = [
    { "allocated":"20", "unallocated":"2", "day":"01/01/2014" },
    { "allocated":"12", "unallocated":"0", "day":"02/01/2014" },
    { "allocated":"2", "unallocated":"18", "day":"03/01/2014" },
    { "allocated":"22", "unallocated":"2", "day":"04/01/2014" },
    { "allocated":"3", "unallocated":"13", "day":"05/01/2014" },
    { "allocated":"6", "unallocated":"2.4", "day":"06/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"07/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"08/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"09/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"10/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"11/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"12/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"13/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"14/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"15/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"16/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"17/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"18/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"19/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"20/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"21/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"22/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"23/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"24/01/2014" },
    { "allocated":"14", "unallocated":"7", "day":"24/05/2014" },
    { "allocated":"14", "unallocated":"7", "day":"25/01/2014" }

];

var groupedByYear = _.groupBy(data, function(item) {
    var dateMoment = moment(item.day,"DD/MM/YYYY");
    return dateMoment.week();
});

var weekDiv = document.getElementById("weeks").innerHTML = JSON.stringify(groupedByYear);
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.3/moment.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
<div id="weeks"></div>
分享给朋友:
您可能感兴趣的文章:
随机阅读: