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

74
Vistas
Compare date and time in array of objects

I'm trying to compare the date and time to manipulate my data. I need to check which is the latest data by checking updated_at key inside object.

Below I have given the scenario.

// below is my data to be manipulat
[{
  "is_latest": "",
  "created_at": "2021-09-21T21:24:05.000Z",
  "updated_at": "2021-09-21T17:53:29.000Z"
}, {
  "is_latest": "",
  "created_at": "2021-09-22T21:24:05.000Z",
  "updated_at": "2021-09-22T17:53:29.000Z"
}, {
  "is_latest": "",
  "created_at": "2021-09-29T21:24:05.000Z",
  "updated_at": "2021-09-29T17:53:29.000Z" // this is the latest data
}]

I'm trying like this, but how to use moment here to compare which is latest.

for (var i = 0; i < data.length; i++) {
  if (data[i].updated_at > data[i + 1].updated_at) {
    data.is_latest = "true"
  }
}

But I'm not getting the expected result as below.

[{
  "is_latest": "false",
  "created_at": "2021-09-21T21:24:05.000Z",
  "updated_at": "2021-09-21T17:53:29.000Z"
}, {
  "is_latest": "false",
  "created_at": "2021-09-22T21:24:05.000Z",
  "updated_at": "2021-09-22T17:53:29.000Z"
}, {
  "is_latest": true,
  "created_at": "2021-09-29T21:24:05.000Z",
  "updated_at": "2021-09-29T17:53:29.000Z"
}]

How can I do this by using map() or reduce()?

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

0

You could locate the latest date by mapping the updated_at field values and spreading them in a Math.max call. After converting the Unix epoch back into a date, you can locate it by using Array.prototype.find and set the is_latest to true.

Note: Since your dates are in ISO 8601 format, converting them back is as easy as calling Date.prototype.toISOString.

const data = [{
  "is_latest": false,
  "created_at": "2021-09-21T21:24:05.000Z",
  "updated_at": "2021-09-21T17:53:29.000Z"
}, {
  "is_latest": false,
  "created_at": "2021-09-22T21:24:05.000Z",
  "updated_at": "2021-09-22T17:53:29.000Z"
}, {
  "is_latest": false,
  "created_at": "2021-09-29T21:24:05.000Z",
  "updated_at": "2021-09-29T17:53:29.000Z" // this is the latest data
}];

const latestDate = new Date(Math.max(...(data.map(({ updated_at }) =>
  new Date(updated_at))))).toISOString();

data.find(({ updated_at }) => updated_at === latestDate).is_latest = true;

console.log(data);
.as-console-wrapper { top: 0; max-height: 100% !important; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

You should check it map and get the max date index. and then set its value to true;

var data = [
{
  "is_latest": "",
  "created_at": "2021-09-21T21:24:05.000Z",
  "updated_at": "2021-09-21T17:53:29.000Z"
},
{
  "is_latest": "",
  "created_at": "2021-09-22T21:24:05.000Z",
  "updated_at": "2021-09-22T17:53:29.000Z"
},
{
  "is_latest": "",
  "created_at": "2021-09-29T21:24:05.000Z",
  "updated_at": "2021-09-29T17:53:29.000Z"
},
{
  "is_latest": "",
  "created_at": "2021-09-30T21:24:05.000Z",
  "updated_at": "2021-09-30T17:53:29.000Z"
}
]
var maxDateIndex = 0;

var newData = data.map((item, index) => {
  if(item.updated_at > data[maxDateIndex].updated_at) 
      maxDateIndex = index;
  item.is_latest = "false";
  return item;
});

newData[maxDateIndex].is_latest = "true";
console.log(newData);

about 4 years ago · Juan Pablo Isaza Denunciar

0

No need to convert to Date objects and back; stick with strings and use localeCompare. Then just reverse sort the values and choose the first one.

let data = [{
    "is_latest": "",
    "created_at": "2021-09-21T21:24:05.000Z",
    "updated_at": "2021-09-21T17:53:29.000Z"
  },
  {
    "is_latest": "",
    "created_at": "2021-09-22T21:24:05.000Z",
    "updated_at": "2021-09-22T17:53:29.000Z"
  },
  {
    "is_latest": "",
    "created_at": "2021-09-29T21:24:05.000Z",
    "updated_at": "2021-09-29T17:53:29.000Z" // this is the latest data
  }
];
const trueVal = true; // change to "true" if you want a string
const falseVal = false; // change to "false" if you want a string
data.sort((a, b) => b.updated_at.localeCompare(a.updated_at));
data[0].is_latest = trueVal;
data.filter(datum => !datum.is_latest).forEach(datum => datum.is_latest = falseVal);
console.log(data);

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