Nested _.max() with lodash
Nested _.max() with lodash
我正在尝试获取对象数组中数组值的最大值和最小值,数组如下所示:
[
{
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},
]
},
]
我需要从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()
})
});
但出于某种原因,这让我回归了——无限。使用嵌套的_.max()运行是否合法?我在D3.js库中使用了这种方法,它运行得很好。
请告知。
内部_.max
将返回具有最大日期的点,但传递给外部_.max
的函数需要一个可以比较的值。当它比较这些值时,它将返回具有最大值的图层。
听起来你想从所有可用层中获得最大点(对吗)?
如果是这样,你可以这样做:
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);
这将返回具有所有点的最大日期的点对象。
这里有一个很好的功能风格的方法:
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中的_.maxBy(max和maxBy),它可以比较日期类型,而无需将其与.getTime()进行转换。
相关文章:
- Fighting with FRP
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- issue with FB.Event.subscribe
- Lodash从集合创建集合
- geolocation-marker.js conflict with markerclusterer.js
- Angular 2.0 with JavaScript or TypeScript?
- Lodash 从值数组中查找数组中的值
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- WebComponentsJS with IE10
- 指令的模板必须只有一个根元素:With restrict E&替换true
- Lodash懒惰链不'It’我不管用
- Lodash映射并返回唯一
- timeago.js with datatable and PHP
- Ajax and Json with Rails
- errors with Javascript try catch
- Nested _.max() with lodash
- Replacing array.filter(x => x.n === n)[0] with lodash
- 下划线where with or condition(下划线、lodash或任何其他解决方案)
- lodash indexBy with not uniq keys
- Lodash: _.forEach with function