7

Let's say I have the following structure

var myArrofObjects =  [
    {prop1:"10", prop2:"20", prop3: "somevalue1"},
    {prop1:"11", prop2:"26", prop3: "somevalue2"},
    {prop1:"67", prop2:"78", prop3: "somevalue3"} ];

I need to find the min and max based on prop2, so here my numbers would be 20 and 78.

How can I write code in Underscore to do that?

7
  • _.min(), _.max() Commented Oct 26, 2015 at 17:32
  • I'd probably just each and set min/max vals; there's no "min/max" just min and max. Sorting would take longer, but you could always sort and take first/last vals. Commented Oct 26, 2015 at 17:32
  • @Andreas. Yes, i know of these functions existence. However, I am asking something more specific, see question. Commented Oct 26, 2015 at 17:34
  • @Dave, I am not sure what min/max is. I need a min and a max. As in, 2 different values. I think the question is pretty clear. Commented Oct 26, 2015 at 17:35
  • 1
    @sarsnake: It's a hypothetical function that returns the min and max at the same time. But anyway, why isn't _min and _max good enough? Commented Oct 26, 2015 at 17:36

5 Answers 5

13

You don't really need underscore for something like this.

Math.max(...arrayOfObjects.map(elt => elt.prop2));

If you're not an ES6 kind of guy, then

Math.max.apply(0, arrayOfObjects.map(function(elt) { return elt.prop2; }));

Use the same approach for minimum.

If you're intent on finding max and min at the same time, then

arrayOfObjects . 
  map(function(elt) { return elt.prop2; }) .
  reduce(function(result, elt) {
    if (elt > result.max) result.max = elt;
    if (elt < result.min) result.min = elt;
    return result;
  }, { max: -Infinity, min: +Infinity });
Sign up to request clarification or add additional context in comments.

3 Comments

You do need Underscore if you want compatibility in older browsers.
Umm, which older browser?
As a note, max returns -Infinity for empty arrays.
7

You can use the _.maxBy to find max value as follows.

var maxValObject = _.maxBy(myArrofObjects, function(o) { return o.prop2; });

or with the iteratee shorthand as follows

var maxValObject = _.maxBy(myArrofObjects, 'prop2');

similarly the _.minBy as well;

Ref: https://lodash.com/docs/4.17.4#maxBy

Comments

2

use _.max and _.property:

var max value = _.max(myArrofObjects, _.property('prop2'));

Comments

2

Use _.max as follows:

var max_object = _.max(myArrofObjects, function(object){return object.prop2})

Using a function as the second input will allow you to access nested values in the object as well.

1 Comment

Probably obvious, but note you need to do max_object.prop2 to get the max value (since this returns the whole object).
0

Underscore

use _.sortBy(..) to sort your object by a property

var sorted = _.sortBy(myArrofObjects, function(item){
    return item.prop2;
});

you will then get a sorted array by your prop1 property, sorted[0] is the min, and sorted[n] is the max

Plain JS

myArrofObjects.sort(function(a, b) {
    return a.prop2 - b.prop2;
})

3 Comments

In the first example, I guess it should be prop2.
I can't really agree with this solution. You are doing a potentially expensive sort only to find the min and max.
yes, the idea is to have a sorted array for other future operations. I don't know OP's use case, but that's what I would have done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.