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.
Can you please help me with writing out the underscore way of doing that?
You don't really need underscore for something like this.
If you're not an ES6 kind of guy, then
Use the same approach for minimum.
If you're intent on finding max and min at the same time, then
You can use the _.maxBy to find max value as follows.
or with the iteratee shorthand as follows
similarly the _.minBy as well;
Ref: https://lodash.com/docs/4.17.4#maxBy
Use _.max as follows:
Using a function as the second input will allow you to access nested values in the object as well.
use _.max and _.property:
Underscore
use _.sortBy(..) to sort your object by a property
you will then get a sorted array by your prop1 property,
sorted[0]
is the min, andsorted[n]
is the maxPlain JS