是否可以结合._max和pluck?组合Lodash方法
查找最大工程和返回最高scoreLabel
var maxScore = _.max(peopleList, scoreLabel);
的对象,但它与勇气结合返回未定义
_.pluck(_.max(peopleList, scoreLabel), scoreLabel);
是否可以结合._max和pluck?组合Lodash方法
查找最大工程和返回最高scoreLabel
var maxScore = _.max(peopleList, scoreLabel);
的对象,但它与勇气结合返回未定义
_.pluck(_.max(peopleList, scoreLabel), scoreLabel);
的_.pluck
的目的是为了获得一个表单域的集合中的每个对象 - 因为_.max
只返回一个对象,你不需要掐,你可以简单地从你有一个对象检索字段:
var maxScore = _.max(peopleList, scoreLabel)[scoreLabel];
以上将会从peopleList
谁拥有最大的scoreLabel
的人,然后检索该人的scoreLabel
值。
或者,你可以交换顺序调用到_.max
和_.pluck
,像这样:
var maxScore = _.max(_.pluck(peopleList, scoreLabel));
这将构建的所有的scoreLabel
值的集合,然后获取最大的一个。
_.max(peopleList, scoreLabel)
返回一个人,而不是人的集合名单,因此您可以简单地使用括号表示法访问其scoreLabel
属性以获取maxScore
。
var maxScore = _.max(peopleList, scoreLabel)[scoreLabel]
它看起来像_.pluck
has also been deprecated in favor of _.map(list, 'property')
最新版本的Lodash。