admin管理员组文章数量:1305712
For example in underscore/lowdash
you can use _.max(list, [iterator], [context])
function to receive one maximum value. But I want to have it returned multiple maximum values if they are all equal.
var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 50}];
_.max(stooges, function(stooge){ return stooge.age; });
=> {name: 'curly', age: 50};
I want to have somthing like this:
var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 50}];
_.multiplemax(stooges, function(stooge){ return stooge.age; });
=> [{name: 'curly', age: 50}, {name: 'larry', age: 50 ];
Using underscore is ok.
For example in underscore/lowdash
you can use _.max(list, [iterator], [context])
function to receive one maximum value. But I want to have it returned multiple maximum values if they are all equal.
var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 50}];
_.max(stooges, function(stooge){ return stooge.age; });
=> {name: 'curly', age: 50};
I want to have somthing like this:
var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 50}];
_.multiplemax(stooges, function(stooge){ return stooge.age; });
=> [{name: 'curly', age: 50}, {name: 'larry', age: 50 ];
Using underscore is ok.
Share Improve this question asked Nov 23, 2013 at 0:29 PolPol 25.6k28 gold badges78 silver badges96 bronze badges 2- Can it always return an array? – megawac Commented Nov 23, 2013 at 0:31
- Obviously... result should be an array as well. – Pol Commented Nov 23, 2013 at 0:32
5 Answers
Reset to default 5Is there any special requirements like you cant not bine multiple functions to do multiplemax. If no, I have 2 solutions in my mind
The simplest solution would be to use _.max to find the max age
of the array, then use _.filter to filter all values that are equal to max age
Another solution is to use _.groupBy to group the array by age
and then get the group with max age
Something like this
function multiplemax(arr, pare) {
var groups = _.groupBy(arr, pare);
var keys = _.keys(groups);
var max = _.max(keys);
return groups[max];
}
more "underscore"
_.mixin({
multiplemax: function(arr, fn) {
var groups = _.groupBy(arr, fn);
var keys = _.keys(groups);
var max = _.max(keys);
return groups[max];
}
})
Or using max
+ filter
function multiplemax(arr, pare) {
var max = _.max(arr, function(v){return v[pare]});
return _.filter(arr, function(v){return v[pare]==max[pare]});
}
Something like this should do the trick.
_.mixin({
multiplymax: function(items, paritor) {
paritor = paritor || _.identity;
var max = paritor(items.pop());
var found = [max];
_.each(items, function(item) {
var val = paritor(item);
if(val > max) {
found = [item];//empty
max = val;
} else if (val === max) {
found.push(item);
}
});
return found;
}
})
Update fixed the broken code ;)
_.multiplymax([{age: 1}, {age:5}, {age:7}, {age:7}, {age:3}], _.property("age")); // [{age:7}, {age:7}]
This should do the trick:
var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 50}];
_.mixin( { multiplemax: function(list, field){
var max = _.max(list, function(item){
return item[field];
});
return _.filter(list, function(item){
return item[field] === max[field];
});
}});
var oldStooges = _.multiplemax(stooges, 'age');
Here is a modern version using TypeScript & ES6:
const multipleMaxBy = <T>(list: T[], iteratee: (element: T) => number) => {
const maxScore = Math.max(...list.map(iteratee));
return list.filter((element) => iteratee(element) === maxScore);
};
Sort it from highest to lowest and take amount
of values:
export const multipleMax = (list: any[], amount: number = 1) => {
return _.take(_.reverse(_.orderBy(list)), amount)
}
本文标签: javascriptGet multiple maximum values from an array of valuesStack Overflow
版权声明:本文标题:javascript - Get multiple maximum values from an array of values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741808174a2398634.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论