You can use your own sort function to have more control over which items are compared.
Array.prototype.deepMax = function(comparator){ if(typeof comparator === 'function'){ var sorted = this.slice(0).sort(comparator); return sorted[sort.length - 1]; } return Math.max.apply(Math, this); };
and you can call it like
var maxWidth = $('img').deepMax(function(a, b){
OR
you can use _. max Underscore , which can be implemented as ...
Array.prototype.max = function(iterator){ if(!iterator && obj[0] === +obj[0]) return Math.max.apply(Math, this); var result = -Infinity, lastComputed = -Infinity; this.forEach(function(value, index){ var computed = iterator ? iterator(value, index, this) : value; computed > lastComputed && (result = value, lastComputed = computed); }); return result; }; var maxWidth = $('img').max(function(val){ return $(val).width();});
Bharath Raja Feb 07 '14 at 5:51 2014-02-07 05:51
source share