3 ms·
You don't need a map, just a reduce (foldl, really). Using underscore, no double calculation: _.reduce(arr, function(p1, p2) { var len = p2.firstN
by jfager 15y ago
You don't need a map, just a reduce (foldl, really). Using underscore, no double calculation:
_.reduce(arr, function(p1, p2) {
var len = p2.firstName.length + p2.lastName.length;
return (len > p1[1]) ? [p2, len] : p1;
},
[null, -1]);
- wooby 15y agoAlso using underscore, with sortBy: _.chain(people) .sortBy(function(person) { return person.firstName.length + person.lastName.length; }) .last() .value()
- mhansen 15y agoFYI, this isn't as general, but I think it shows off underscore quite nicely: _(people).max(function(person) { return person.firstName.length + person.lastName.length; });