4

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

kumar
  • 81
  • 5
  • You are trying to compare a string date format. So that will not work. You can the idea in here on how to compare 2 dates. https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript – Carljul Sep 29 '21 at 14:29
  • 2
    why don't you sort the data by descending order and get the first object – Umer Abbas Sep 29 '21 at 14:31
  • You can compare dates as strings, as long as they are in the same format, and that format is ordered from least specific to most (i.e., ISO 8601), as these are. You should use [`localeCompare`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare) rather than `>` or `<` though. – Heretic Monkey Sep 29 '21 at 14:32
  • 1
    You don't want to use `map` though; you want to use `reduce`. – Heretic Monkey Sep 29 '21 at 14:35
  • I am new to this es6..could you please help to achieve that expected result. – kumar Sep 29 '21 at 14:40
  • What is the expected result? – Heretic Monkey Sep 29 '21 at 14:47
  • Thank you response... i mentioned it at the end of the question.. – kumar Sep 29 '21 at 14:48
  • It has changed since it was first posted; before it had empty strings, now it has `"false"` (a string value), whereas the latest entry has `true` as a Boolean value, not as a string, which is how your original code set it. – Heretic Monkey Sep 29 '21 at 14:50

3 Answers3

1

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; }
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
1

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);
Nbody
  • 1,168
  • 7
  • 33
1

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);
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122