简体   繁体   English

获取最近的日期,下划线

[英]Get most recent date, underscore

Got this:得到这个:

 [{
  "title": "qq",
    "dateValuePair": {
      "date": "2016-04-29T22:00:00.000Z",
      "value": 243500.3491
     }
  },
  {
    "title_sv": "ccc",
    "dateValuePair": {
      "date": "2016-03-29T22:00:00.000Z",
      "value": 243500.3491
     }
 }]

I need a function that returns the object with the most recent date.我需要一个函数来返回具有最近日期的对象。

  {
    "title_sv": "ccc",
    "dateValuePair": {
      "date": "2016-03-29T22:00:00.000Z",
      "value": 243500.3491
     }
 }

Is there some neat one line underscore trick to do this?是否有一些简洁的单行下划线技巧来做到这一点?

You can use _.max :您可以使用_.max

var array = [{
  "title": "qq",
    "dateValuePair": {
      "date": "2016-04-29T22:00:00.000Z",
      "value": 243500.3491
     }
  },
  {
    "title_sv": "ccc",
    "dateValuePair": {
      "date": "2016-03-29T22:00:00.000Z",
      "value": 243500.3491
     }
 }];

var result = _.max(array, function(el) { 
  return new Date(el.dateValuePair.date).getTime();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM