[Vuejs]-Vue Filtered Object Array by month

0๐Ÿ‘

โœ…

So if I understand you correctly you want to group your items by the month value which is stored in the property created_date. The following code will do that for you.

var items = [
{
  "id": 1766013,
  "created_date": "2019-01-28 12:54:23",
  "amount": 10000,
  "type": "payin",
  "status": "done",
  "method": "m_visamc"
},
{
  "id": 1766012,
  "created_date": "2019-03-28 12:54:08",
  "amount": 300000,
  "type": "payin",
  "status": "error",
  "method": "m_visamc"
},
{
  "id": 1766011,
  "created_date": "2019-02-28 12:53:31",
  "amount": 6000,
  "type": "payin",
  "status": "done",
  "method": "m_visamc"
},
{
  "id": 1766012,
  "created_date": "2019-02-26 12:53:36",
  "amount": 6000,
  "type": "payin",
  "status": "done",
  "method": "m_visamc"
}];

var groupByCreatedDateMonth = array =>
	array.reduce((objectsByKeyValue, obj) => {
	    var dateString = obj.created_date;
            var date = new Date(dateString);
            var value = date.getMonth();
      
	    objectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(obj);
	    return objectsByKeyValue;
	}, {});
            
var result = groupByCreatedDateMonth(items);

console.log(result);

Leave a comment