How to group JSON data dates using Underscore.js library?
I have the 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 make GroupBy a "WEEK" of this data. So, I need the data above in the form of a week. How is this possible using the underscore.js library? thanks in advance
+3
source to share
1 answer
Using moment.js and underscore _.groupBy
you can 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>
+5
source to share