I have an array of objects. Each object in the array has a date property. I am trying to get the largest (last) date from an array.
Here is the array:
var sensorsData = [{
Id: 1,
MeasureDate: "2017-08-20T09:52:32"
}, {
Id: 2,
MeasureDate: "2017-08-20T09:54:35"
}, {
Id: 3,
MeasureDate: "2017-08-20T09:56:13"
}];
And here is the function: select the largest date from the array above:
function updateLatestDate(sensorsData) {
return new Date(Math.max.apply(null, sensorsData.map(function(e) {
return new Date(e.MeasureDate);
}))).toISOString();
}
The result that I get from the function updateLatestDateis:
2017-08-20T06:56:13.000Z
but this is strange, because since you do not see any of the properties in the sensorData objects, there is no date returned from the function updateLatestDate.
Here is the FIDDLER .
Any idea why the updateLatestDate function returns an incorrect result?
source
share