Home > Blockchain >  How to get element of least time in array?
How to get element of least time in array?

Time:08-24

I have an array of of objects:

const sessions = [
{videoStartTime: "2022-08-23T12:05:28.000Z"},
{videoStartTime: "2022-08-23T11:39:51.000Z"},
{videoStartTime: "2022-08-23T10:51:03.000Z"},
{videoStartTime: "2022-08-22T15:49:44.000Z"},
{videoStartTime: "2022-08-22T15:34:42.000Z"},
{videoStartTime: "2022-08-22T15:25:58.000Z"},
{videoStartTime: "2022-08-17T11:11:05.000Z"}
]

And I need to write a function that get an item of the least time in array.

Output shoulde be like this: {videoStartTime: "2022-08-17T11:11:05.000Z"}

Thanks!

CodePudding user response:

Sorting the whole list for the minimum is very slow; you can use .reduce() instead to linear search:

const sessions = [
  {videoStartTime: "2022-08-23T12:05:28.000Z"},
  {videoStartTime: "2022-08-23T11:39:51.000Z"},
  {videoStartTime: "2022-08-23T10:51:03.000Z"},
  {videoStartTime: "2022-08-22T15:49:44.000Z"},
  {videoStartTime: "2022-08-22T15:34:42.000Z"},
  {videoStartTime: "2022-08-22T15:25:58.000Z"},
  {videoStartTime: "2022-08-17T11:11:05.000Z"}
];

const earliest = sessions.reduce((prev, curr) => {
  if (prev == null)
    return curr;
  
  if (Date.parse(curr.videoStartTime) < Date.parse(prev.videoStartTime))
    return curr;
  else
    return prev;
}, null);

console.log(earliest);

CodePudding user response:

const sessions = [
{videoStartTime: "2022-08-23T12:05:28.000Z"},
{videoStartTime: "2022-08-23T11:39:51.000Z"},
{videoStartTime: "2022-08-23T10:51:03.000Z"},
{videoStartTime: "2022-08-22T15:49:44.000Z"},
{videoStartTime: "2022-08-22T15:34:42.000Z"},
{videoStartTime: "2022-08-22T15:25:58.000Z"},
{videoStartTime: "2022-08-17T11:11:05.000Z"}
]

var orderedDates = sessions.sort(function(a, b) {
  return Date.parse(a.videoStartTime) - Date.parse(b.videoStartTime);
});
console.log(orderedDates[0]);

CodePudding user response:

You can sort the array and get the first index

const sessions = [{
    videoStartTime: "2022-08-23T12:05:28.000Z"
  },
  {
    videoStartTime: "2022-08-23T11:39:51.000Z"
  },
  {
    videoStartTime: "2022-08-23T10:51:03.000Z"
  },
  {
    videoStartTime: "2022-08-22T15:49:44.000Z"
  },
  {
    videoStartTime: "2022-08-22T15:34:42.000Z"
  },
  {
    videoStartTime: "2022-08-22T15:25:58.000Z"
  },
  {
    videoStartTime: "2022-08-17T11:11:05.000Z"
  }
]

const newSessions = [...sessions].sort((a, b) => {
  return new Date(a.videoStartTime).getTime() - new Date(b.videoStartTime).getTime()
})

console.log(newSessions[0])

console.log(`Expected: '{videoStartTime: "2022-08-17T11:11:05.000Z"}'`)

CodePudding user response:

I've managed to use reduce function to get the object with the lowest value in this scenario

const lowest = sessions.reduce((lowest, current) => {
  return new Date(current.videoStartTime).getTime() < new Date(lowest.videoStartTime).getTime() ? current : lowest;
},sessions[0])

We initiate the loop with first value from the sessions array and compare every entry with the currently lowest found value.

We also use getTime() method do get the date's timestamp in miliseconds that we can actually compare.

CodePudding user response:

sessions.sort((a, b) => a.videoStartTime - b.videoStartTime);
console.log(sessions[0])

  • Related