Reputation: 53890
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()
})
});
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.
Please advise.
Upvotes: 3
Views: 12407
Reputation: 101
You can use _.maxBy from lodash, (max and maxBy)it compares date type without the need of transfor them with .getTime().
Upvotes: 3
Reputation: 101730
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);
Upvotes: 7