JSFiddle - React, Tailwind, and code Playground
JavaScript
var data = [
{
"date": '2017-07-03',
"description": 'Receipt description',
"amount": 300
},
{
"date": '2017-07-08',
"description": 'Receipt description',
"amount": 300
},
{
"date": '2017-08-05',
"description": 'Receipt description',
"amount": 300
},
{
"date": '2017-08-03',
"description": 'Receipt description',
"amount": 300
},
{
"date": '2016-07-03',
"description": 'Receipt description',
"amount": 300
},
{
"date": '2016-07-08',
"description": 'Receipt description',
"amount": 300
}
];
var sorted = data.sort(function(a, b) {
return new Date(a.date) - new Date(b.date);
});
var byYearAndByMonth = {};
_.each(sorted, function(item) {
var year = item.date.substring(0,4)
var month = item.date.substring(5,7)
if (typeof byYearAndByMonth[year] === "undefined") {
byYearAndByMonth[year] = {};
}
if (typeof byYearAndByMonth[year][month] === "undefined") {
byYearAndByMonth[year][month] = [];
}
byYearAndByMonth[year][month].push(item);
});
console.log(byYearAndByMonth);