Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

228
Vistas
Angular - Count all dates per month & return month & count array

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",
]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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);

about 4 years ago · Juan Pablo Isaza Denunciar

0

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]]
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda