javascript 使用 lodash 嵌套 _.max()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28855505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:39:11  来源:igfitidea点击:

Nested _.max() with lodash

javascriptarrayslodash

提问by Sergei Basharov

I am trying to get max and min values of arrays values inside objects array which looks like this:

我试图在对象数组中获取数组值的最大值和最小值,如下所示:

[
    {
        id: 1,
        enabled: false,
        layer: 'Mood',
        color: '#f16c63',
        points: [
            {date: '2013-01-02', value: 20},
            {date: '2013-02-02', value: 15},
            {date: '2013-03-12', value: 24},
            {date: '2013-03-23', value: 18},
            {date: '2013-03-24', value: 22},
            {date: '2013-04-09', value: 12},
            {date: '2013-06-13', value: 16},
            {date: '2013-06-14', value: 20},
        ]
    },
    {
        id: 2,
        enabled: true,
        layer: 'Performance',
        color: '#698bfc',
        points: [
            {date: '2013-01-02', value: 15},
            {date: '2013-02-02', value: 24},
            {date: '2013-03-12', value: 29},
            {date: '2013-03-23', value: 21},
            {date: '2013-03-24', value: 20},
            {date: '2013-04-09', value: 17},
            {date: '2013-06-13', value: 25},
            {date: '2013-06-14', value: 21},
        ]
    },
    {
        id: 3,
        enabled: false,
        layer: 'Fatigue',
        color: '#e1fc6a',
        points: [
            {date: '2013-01-02', value: 32},
            {date: '2013-02-02', value: 27},
            {date: '2013-03-12', value: 30},
            {date: '2013-03-23', value: 31},
            {date: '2013-03-24', value: 27},
            {date: '2013-04-09', value: 15},
            {date: '2013-06-13', value: 20},
            {date: '2013-06-14', value: 18},
        ]
    },
    {
        id: 4,
        enabled: true,
        layer: 'Hunger',
        color: '#63adf1',
        points: [
            {date: '2013-01-02', value: 12},
            {date: '2013-02-02', value: 15},
            {date: '2013-03-12', value: 13},
            {date: '2013-03-23', value: 17},
            {date: '2013-03-24', value: 10},
            {date: '2013-04-09', value: 14},
            {date: '2013-06-13', value: 12},
            {date: '2013-06-14', value: 11},
        ]
    },
]

I need to get the max and the min values from the pointsarrays. Seems like I can do something like this for max and similar for min values:

我需要从points数组中获取最大值和最小值。似乎我可以对最大值执行类似的操作,对最小值执行类似的操作:

                   var maxDate = _.max(dataset, function (area) {
                   return _.max(area.points, function (point) {
                       console.log(new Date(point.date).getTime())
                       return new Date(point.date).getTime()
                   })

               });

But for some reason this returns me -Infinity. Is it legit at all to use nested _.max() runs? I used this approach with D3.js library which worked just fine.

但出于某种原因,这使我返回-Infinity。使用嵌套的 _.max() 运行是否合法?我将这种方法与 D3.js 库一起使用,效果很好。

Please advise.

请指教。

回答by JLRishe

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

内部_.max将返回具有最大日期的点,但您传递给外部的函数_.max需要一个可以比较的值。当它比较这些值时,它将返回具有最大值的图层。

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

回答by Lorand-Edmond Iszlai

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

您可以使用 lodash 中的 _.maxBy,(max 和 maxBy)它比较日期类型而无需使用 .getTime() 转换它们。