Home > database >  Compare date and time in array of objects
Compare date and time in array of objects

Time:09-30

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()?

CodePudding user response:

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);

CodePudding user response:

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);

CodePudding user response:

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; }

  • Related