用lodash嵌套_.max() [英] Nested _.max() with lodash

查看:92
本文介绍了用lodash嵌套_.max()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 points arrays. Seems like I can do something like this for max and similar for min values:

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

               });

但由于某种原因,这会让我 - 无限。使用嵌套的_.max()运行是否合法?我使用这种方法与D3.js库工作得很好。

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.

请指教。

推荐答案

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

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

这篇关于用lodash嵌套_.max()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆