Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
js getting months & years from array

I have an array of objects:

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' },
  ]

How can I filter array of objects for each month with year, like :

'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' }
]

Thanks in advance!:)

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I would do something like this:

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

// Usage
filterArr("10", "2021");

EDIT: If you want to use the name of the month try this:

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 Relatório

0

You can try something like this:

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

Refs: Array.prototype.reduce()

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use forEach method to iterate from the array and save everything in an object. Like this:

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda