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

115
Vistas
Map and sort data by date

I retrieve an array of objects and I need to format it to make it acceptable for Google Charts. The initial data looks like so

[
   {
      "itemOne":111,
      "itemTwo":1,
      "itemThree":"2022-02-01T00:00:00.000Z"
   },
   {
      "itemOne":222,
      "itemTwo":2,
      "itemThree":"2022-01-01T00:00:00.000Z"
   },
   {
      "itemOne":333,
      "itemTwo":3,
      "itemThree":"2021-12-01T00:00:00.000Z"
   },
]

To start the formatting, I do the following

const chartData = data.lastYear.map(
    (item) => ([item.itemThree, item.itemTwo, item.itemOne]),
);

Which now leaves me with this.

[
    ["2022-02-01T00:00:00.000Z", 1, 111],
    ["2022-01-01T00:00:00.000Z", 2, 222],
    ["2021-12-01T00:00:00.000Z", 3, 333],
]

I have been trying however to sort the above by date. My current attempt is pretty bad, and obviously doesnt work.

 const chartData = data.lastYear.map(
    (item) => ([item.itemThree, item.itemTwo, item.itemOne]),
  ).sort((a, b) => {
    return new Date(b.date) - new Date(a.date);
  });
  

So what would be the best way to sort everything by date?

Thanks

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

let data = [
   {
      "itemOne":111,
      "itemTwo":1,
      "itemThree":"2022-02-01T00:00:00.000Z"
   },
   {
      "itemOne":222,
      "itemTwo":2,
      "itemThree":"2022-01-01T00:00:00.000Z"
   },
   {
      "itemOne":333,
      "itemTwo":3,
      "itemThree":"2021-12-01T00:00:00.000Z"
   },
]
data.sort((a,b)=>{
    return new Date(b.itemThree) - new Date(a.itemThree);
})

This sorts your data array according to date. Hope this is helpful

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