Básicamente, contar registros por mes, donde cada registro tiene una fecha de creación.
Necesita una matriz que contenga el nombre del mes + recuento como variables.
Solo logré hacerlo con SQL.
Aquí está mi matriz con formato de fecha y hora:
[ "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", ]Use Array.prototype.reduce() para agrupar el mes y realizar el conteo.
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);Usualmente usamos el objeto json para resolver este tipo de preguntas. Porque el objeto json podría tener un índice de cadena.
Json:
{ "10":{"cnt" : 4}, "9":{"cnt" : 5}, }Formación:
[["10", 4],["9", 5]]use forEach() yentries( ) para recuperar su matriz
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]]