javascript - underscore to find min and max of object value -
as per tutorial here,
a collection can either array or object, associate array in javascript
does mean functions under collection equally applicable object literals. example, wanted pick values based on condition. say,
var obj = { "1": {id: 1, val: 2}, "2": {id: 2, val: 5}, "3": {id: 3, val: 8}, "4": {id: 4, val: 1} } i want find max , min of val field. looking @ api, thinking use pluck array of val, min , max.
- can apply pluck object (as api example show use in array of objects)
- is there better way?
thanks.
does mean functions under collection equally applicable object literals?
yes.
can apply pluck object (as api example show use in array of objects)
have tried it? yes, can, array.
is there better way?
math.min.apply(null, _.pluck(obj, "val")) (or _.min(_.pluck(obj, "val"))) getting minimum value fine. yet, if wanted whole object (with id) might use iterator parameter of min/max:
var lowest = _.min(obj, function(o){return o.val;});
Comments
Post a Comment