Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
Angular: cuente todas las fechas por mes y devuelva el mes y cuente la matriz

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", ]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

página de demostración

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]]
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!