Nested _.max() with lodash

末鹿安然 提交于 2019-12-05 21:03:48

The inner _.max is going to return the point with the maximum date, but the function you're passing to the outer _.max expects a value that can be compared. When it compares those values, it will return the layer with the maximum value.

It sounds like you want to get the maximum point out of all the available layers (is this right)?

If so, you can do this:

function pointDate(point) {
    console.log(new Date(point.date).getTime())
    return new Date(point.date).getTime()
}

var maxDate = _.max(_.map(dataset, function (area) {
    return _.max(area.points, pointDate);
}), pointDate);

This will return the point object with the maximum date across all points.

Here's a nice functional-style approach:

function pointDate(point) {
    console.log(new Date(point.date).getTime())
    return new Date(point.date).getTime()
}

var maxDate = _(dataset).map('points').flatten().max(pointDate);

You can use _.maxBy from lodash, (max and maxBy)it compares date type without the need of transfor them with .getTime().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!