2016-08-09 42 views
1

diese Got:Erhalten jüngste Datum, unterstreichen

[{ 
    "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 
    } 
}] 

Ich brauche eine Funktion, die das Objekt mit dem jüngsten Datum zurückgibt.

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

Gibt es einen sauberen Unterstrich?

+0

[ 'arr.slice() sort ((a, b) => + new Date (a.dateValuePair.date) <+ new Date (b.dateValuePair .date)) [0] '] (https://jsfiddle.net/qjhbmt1k/) – Tushar

Antwort

1

können Sie _.max benutzen.

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(); 
});