Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

75
Views
Compare la fecha y la hora en una matriz de objetos

Estoy tratando de comparar la fecha y la hora para manipular mis datos. Necesito verificar cuáles son los datos más recientes al verificar la clave updated_at dentro del objeto.

A continuación he dado el escenario.

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

Estoy intentando así, pero cómo usar el momento aquí para comparar cuál es el último.

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

Pero no obtengo el resultado esperado como se muestra a continuación.

 [{ "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" }]

¿Cómo puedo hacer esto usando map() o reduce()?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puede ubicar la última fecha mapeando los valores del campo updated_at y distribuyéndolos en una llamada Math.max . Después de volver a convertir la época de Unix en una fecha, puede ubicarla utilizando Array.prototype.find y establecer is_latest en true .

Nota: dado que sus fechas están en formato ISO 8601, volver a convertirlas es tan fácil como llamar a 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 Report

0

Debe consultar el mapa y obtener el índice de fecha máxima. y luego establezca su valor en verdadero;

 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 Report

0

No es necesario convertir a objetos de Date y viceversa; quédese con las cadenas y use localeCompare . Luego simplemente invierta la ordenación de los valores y elija el primero.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!