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

150
Views
js obteniendo meses y años de la matriz

Tengo una serie de objetos:

 let arr = [ {date: '2021/01/23', a1: 'text1', a2: 'text2' }, {date: '2021/11/21', a1: 'text1', a2: 'text2' }, {date: '2020/12/22', a1: 'text1', a2: 'text2' }, {date: '2021/12/08', a1: 'text1', a2: 'text2' }, {date: '2020/12/14', a1: 'text1', a2: 'text2' }, {date: '2021/10/25', a1: 'text1', a2: 'text2' }, {date: '2021/12/04', a1: 'text1', a2: 'text2' }, ]

¿Cómo puedo filtrar una matriz de objetos para cada mes con año, como:

 'December 21' = [ {date: '2021/12/04', a1: 'text1', a2: 'text2' }, {date: '2021/12/08', a1: 'text1', a2: 'text2' } ] 'January 21' = [ {date: '2021/01/23', a1: 'text1', a2: 'text2' } ] 'December 20' = [ {date: '2020/12/22', a1: 'text1', a2: 'text2' } ]

¡Gracias por adelantado!:)

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Yo haría algo como esto:

 const filterArr = (month, year) => arr.filter(x => { const splitDate = x.date.split('/'); return splitDate[0] === year && splitDate[1] === month; }); // Usage filterArr("10", "2021");

EDITAR: si desea usar el nombre del mes, intente esto:

 const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; const monthNameToNumber = (month) => (monthNames.indexOf(month) + 1) .toString().padStart(2, '0'); // Usage monthNameToNumber("December"); // "12"
about 4 years ago · Juan Pablo Isaza Report

0

Puedes intentar algo como esto:

 // Your input let arr = [ {date: '2021/01/23', a1: 'text1', a2: 'text2' }, {date: '2021/11/21', a1: 'text1', a2: 'text2' }, {date: '2020/12/22', a1: 'text1', a2: 'text2' }, {date: '2021/12/08', a1: 'text1', a2: 'text2' }, {date: '2020/12/14', a1: 'text1', a2: 'text2' }, {date: '2021/10/25', a1: 'text1', a2: 'text2' }, {date: '2021/12/04', a1: 'text1', a2: 'text2' }, ]; // Month dictionary const monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; // Get what you want const result = arr.reduce((acc, el) => { // Extract year & month from date const [ year, month ] = el.date.split('/'); // Get label in 'Month YY' format const label = monthNames[+month - 1] + ' ' + year.slice(2); // Add object key to result if necessary if (!acc[label]) acc[label] = []; // Save element acc[label].push(el); return acc; }, {}); console.log(result);

Referencias: Array.prototype.reduce()

about 4 years ago · Juan Pablo Isaza Report

0

Puede usar el método forEach para iterar desde la matriz y guardar todo en un objeto. Como esto:

 let arr = [ {date: '2021/01/23', a1: 'text1', a2: 'text2' }, {date: '2021/11/21', a1: 'text1', a2: 'text2' }, {date: '2020/12/22', a1: 'text1', a2: 'text2' }, {date: '2021/12/08', a1: 'text1', a2: 'text2' }, {date: '2020/12/14', a1: 'text1', a2: 'text2' }, {date: '2021/10/25', a1: 'text1', a2: 'text2' }, {date: '2021/12/04', a1: 'text1', a2: 'text2' }, ]; filteredArr = {}; arr.forEach((obj) => { const newDate = new Date(obj.date); const stringMonthAndYear = newDate.toLocaleString('default', { month: 'long' }) + ' ' + newDate.getFullYear(); if (filteredArr[stringMonthAndYear] && filteredArr[stringMonthAndYear].length) { filteredArr[stringMonthAndYear].push(obj); } else { filteredArr[stringMonthAndYear] = [obj]; } }); console.log(filteredArr);

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!