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
Add a ment  | 

5 Answers 5

Reset to default 5

Is 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