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
Underscore js group by each month on each year in single level

This question is the extention of this question. which is here Underscore js group by each month on each year This is giving the expected result and the code which i am using is here.

   const months = [
        'jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'
    ]
    const result = flashMessage.reduce((res:any, item:any) => {
        const date = new Date(item.created_at)
        const year = date.getFullYear();
        const month = months[date.getMonth()];
        const existingYear = res[year] || {}
        res[year] = existingYear
        const updated  = [...(existingYear[month] || []), item]
        res[year][month] = updated
        return res
     }, {});

This is the answer which it is producing now.

{
  "2022": {
    "jan": [
      {
        "id": 3,
        "created_by": 1,
        "created_at": "2022-01-31T07:00:01.880Z"
      },
      {
        "id": 2,
        "created_by": 1,
        "created_at": "2022-01-31T07:00:01.880Z"
      },
      {
        "id": 1,
        "created_by": 1,
        "created_at": "2022-01-31T07:00:01.880Z"
      }
    ]
  }
}

What if i want the result in a single level.

{
  "2022 jan": [
      {
        "id": 3,
        "created_by": 1,
        "created_at": "2022-01-31T07:00:01.880Z"
      }
      {
        "id": 2,
        "created_by": 1,
        "created_at": "2022-01-31T07:00:01.880Z"
      },
      {
        "id": 1,
        "created_by": 1,
        "created_at": "2022-01-31T07:00:01.880Z"
      }
    ]
  "2022 feb": [
      {
        "id": 3,
        "created_by": 1,
        "created_at": "2022-02-31T07:00:01.880Z"
      }
    ]
  "2021 feb": [
      {
        "id": 3,
        "created_by": 1,
        "created_at": "2021-02-31T07:00:01.880Z"
      }
    ]
}

Is there any way i can do this?

about 4 years ago · Juan Pablo Isaza
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