Why do I get wrong result from function?

前端 未结 2 539
伪装坚强ぢ
伪装坚强ぢ 2021-01-26 01:48

I has this objects array:

var data=[{Id:540, MeasureDate:\"2016-06-27T15:06:47\"},
          {Id:541, MeasureDate:\"2016-06-27T12:24:39\"}];

I

2条回答
  •  盖世英雄少女心
    2021-01-26 02:26

    As mentioned by @elliot moment can help you. Check this solution:

    (function() {
    
      var data = [{
        Id: 540,
        MeasureDate: "2016-06-27T13:06:47"
      }, {
        Id: 541,
        MeasureDate: "2016-06-27T12:24:39"
      }, {
        Id: 540,
        MeasureDate: "2016-06-27T13:16:47"
      }];
    
      function getMaxInArray(items, max) {
        if (items.length) {
          var item = items.splice(0, 1)[0];
          return getMaxInArray(items, Math.max(item, !!max ? max : item));
        }
        return max;
      }
    
      var dates = data.map(function(x) { return moment(x.MeasureDate); });
      var latest = moment(getMaxInArray(dates));
    
      console.log(latest.format());
    
    }());
    

    http://plnkr.co:80/oeMDJYzDGkgm4cX4NIpo

提交回复
热议问题