Home > Net >  Angular - Count all dates per month & return month & count array
Angular - Count all dates per month & return month & count array

Time:10-30

Basically counting records per month, where each record has a Create Date.

Need an array that contains month name count as variables.

Only managed to do it with SQL

Here's my array with Date Time format

[ "2021-10-06", "2021-10-06", "2021-10-06", "2021-10-06", "2021-9-06", "2021-9-06", "2021-9-06", "2021-9-06", "2021-9-06", ]

CodePudding user response:

Use Array.prototype.reduce() for grouping month and perform count.

var monthNames = [
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
];

var dates = [ "2021-10-06", "2021-10-06", "2021-10-06", "2021-10-06", "2021-9-06", "2021-9-06", "2021-9-06", "2021-9-06", "2021-9-06"];

var group = dates.reduce(function (r, o){
   var date = new Date(o); 
   var month = date.getMonth();
   var monthName = monthNames[month];
   (r[monthName]) ? r[monthName].count   : r[monthName] = { monthName: monthName, count: 1 };
return r; 
}, {});

var result = Object.keys(group).map((key) => group[key]);

console.log(result);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Demo page

We usually use json object to solve this kind of question. Because json object could have string index.

Json:

{
  "10":{"cnt" : 4},
  "9":{"cnt" : 5},
}

Array:

[["10", 4],["9", 5]]

use forEach() and entries() to retrieve your array

var dates = [ "2021-10-06", "2021-10-06", "2021-10-06", "2021-10-06", "2021-9-06", "2021-9-06", "2021-9-06", "2021-9-06", "2021-9-06"];

let array_month = [];
for (let i = 0; i < dates.length; i  ) {
    const month = dates[i].split('-')[2];
    array_month.push(month);
}
// array_month = ["10", "10", "10", "10", "9", "9", "9", "9", "9"]

let obj = {};
array_month.forEach(function (x) {
    // The expression counts[x] || 0 returns the value of counts[x] if it is set, otherwise 0. Then just add one and set it again in the object and the count is done.
    obj[x] = (obj[x] || 0)   1;
});
//final = {
//  10: 4,
//  9: 5
//}

//convert object to array
var result = Object.entries(obj);
console.log(result);
//[[MonthName, MonthCount]]
//[["9", 5], ["10", 4]]
  • Related