What is the elegant way to get the latest date from array of objects in client side?

后端 未结 9 1782
面向向阳花
面向向阳花 2020-11-27 06:17

I use angularjs in project.

I get array of objects from the server. Each object contains few properties and one of them is date property.

Here is the Array

相关标签:
9条回答
  • 2020-11-27 06:53

    Further to @Travis Heeter's answer, this returns the object that contains the latest date:

    array.reduce((a, b) => (a.MeasureDate > b.MeasureDate ? a : b));

    A more robust solution perhaps might be convert the strings into Date objects every time. Could be noticeably slower if dealing with (very) large arrays:

    array.reduce((a, b) => {
      return new Date(a.MeasureDate) > new Date(b.MeasureDate) ? a : b;
    })
    
    0 讨论(0)
  • 2020-11-27 06:55

    If you want to get the whole Object, not just the date...

    If OP's array of Objects was assigned to a this is how you get the Object with the most recent date:

    var mostRecentDate = new Date(Math.max.apply(null, a.map( e => {
       return new Date(e.MeasureDate);
    })));
    var mostRecentObject = a.filter( e => { 
        var d = new Date( e.MeasureDate ); 
        return d.getTime() == mostRecentDate.getTime();
    })[0];
    
    1. a.map gets the dates from the array of objects.
    2. new Date is applied to each date, making Date Objects
    3. Math.max.apply finds the most recent
    4. We have found the most recent Date, now we need the object.
    5. a.filter loops through the original a array.
    6. We need some way to compare dates, so we use .getTime(), which returns the number of milliseconds since 01/01/1970. This will account for time - if it's defined - as well as date.
    7. When the correct date is found, true is returned, and .filter gives us just that object.

    Note: This solution is an extension of @archyqwerty's answer above. Their solution gave only the most recent date from an array of objects, this solution gives you the whole Object that the date was a member of.

    0 讨论(0)
  • 2020-11-27 06:56
    function getLatestDate(data) {
       // convert to timestamp and sort
       var sorted_ms = data.map(function(item) {
          return new Date(item.MeasureDate).getTime()
       }).sort(); 
       // take latest
       var latest_ms = sorted_ms[sorted_ms.length-1];
       // convert to js date object 
       return new Date(latest_ms);
    }
    
    var data = [{MeasureDate: "2014-10-04T16:10:00"},
                {MeasureDate: "2013-10-04T16:10:00"},
                {MeasureDate: "2012-10-04T16:10:00"}];
    
    getLatestDate(data).toString(); // "Sat Oct 04 2014 18:10:00 GMT+0200 (CEST)"
    

    This function returns the latest date as a JavaScript date Object. You can also turn it into an ISO-String (the format of your source data) with the Date-Object method toISOString().

    var date_str = "2012-10-04T16:10:00";
    (new Date(date_str)).toISOString(); // "2012-10-04T16:10:00.000Z"
    

    As you can see the result of the method includes always zero milliseconds in the end. If you need your original ISO data-string as a result, you may want to go with the following function:

    function getLatestDate2(data) {
    
       var sorted = data.map(function(item) {
          var MeasureDate = item.MeasureDate;
          return {original_str: MeasureDate,
                  in_ms: (new Date(MeasureDate)).getTime()}
       }).sort(function(item1, item2) {
          return (item1.in_ms < item2.in_ms)
       }); 
    
       // take latest
       var latest = sorted[0];
    
       return latest.original_str;
    }
    
    getLatestDate2(data); // "2014-10-04T16:10:00"
    
    0 讨论(0)
  • 2020-11-27 06:59

    Inspired by many of the suggestions and comments in this thread, here is another solution for the problem. It's very fast, since there is no date object convertion.

    function getLatestDate(xs) {
       if (xs.length) {
          return xs.reduce((m, i) => (i.MeasureDate > m) && i || m, "")
                   .MeasureDate;
       }
     }
    

    Here's a version for Browser's not supporting arrow functions:

    function getLatestDateSave(xs) {
       if (xs.length) {
          return xs.reduce(function(m, i) {
             return (i.MeasureDate > m) && i || m;
          }, "").MeasureDate;
       }
     }
    
    0 讨论(0)
  • 2020-11-27 07:06

    This how I picked lasest/highest date

    var maxLastseen= obj.sort((a,b) => new Date(b.lastS).getTime() - new Date(a.lastS).getTime())[0];
    
    0 讨论(0)
  • 2020-11-27 07:07
            var dates = []; 
    
            dates.push(new Date("2019/06/25")); 
            dates.push(new Date("2019/06/26")); 
            dates.push(new Date("2019/06/27")); 
            dates.push(new Date("2019/06/28")); 
    
            function GFG_Fun() { 
                var maximumDate=new Date(Math.max.apply(null, dates)); 
                var minimumDate=new Date(Math.min.apply(null, dates)); 
            } 
    
    0 讨论(0)
提交回复
热议问题