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

144
Vistas
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 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