Conversation
The maxVal function will iterate through each of the properties in an object and select for the property that has the greatest numeric value. If there are no properties with in the object that contain a numeric value the generic result ' ["key",-Infinity] ' is returned. The function minVal operates in the same manner except that it selects for the property with the least numeric value and it's default return value is ' ["key", Infinity] '.
This offers the user much shorter syntax than than ' _.max(obj,function(property){property;}); ' and also provides the key where the max value resides. I also included it's complimentary minVal function as well.
These functions provide a direct cross-over for ruby users looking for ' hash.max_by{|k,v| v} ' & ' hash.min_by{|k,v| v} '. Since these functions are specific to objects, I thought it would be appropriate to put this in object selectors.
|
You should be checking whether the values are numbers, or at least whether comparisons among them make sense. You might also want to look at the way core Underscore handles passing in comparators as "iteratees" in |
|
Hey thank you for taking a look, I did have a look at the ._min and ._max but I was just thinking it might be nice to have short hand way to do that for objects. But I should generalize it to sort by some function. Also I did test it on objects containing strings and functions as well as numbers and it works correctly for me, is that not the case in general? Sorry I'm a bit new to coding, but I thought this might be a nice shortcut. |
|
Slapping an "after modules" onto this because it seems like it might require some more thought and also, there is not much code so it shouldn't be too much effort to fix some merge conflicts when continuing this branch. |
The maxVal function will iterate through each of the properties in an object and select for the property that has the greatest numeric value. If there are no properties within the object that contain a numeric value the generic result ' ["key",-Infinity] ' is returned. The function minVal operates in the same manner except that it selects for the property with the least numeric value and it's default return value is ' ["key", Infinity] '.
This offers the user much shorter syntax than than ' _.max(obj,function(property){ return property; }); ' and also provides the key where the max value resides. I also included it's complimentary minVal function as well.
These functions provide a direct cross-over for ruby users looking for ' hash.max_by{|k,v| v} ' & ' hash.min_by{|k,v| v} '. Since these functions are specific to objects, I thought it would be appropriate to put this in object selectors.