Hacker News new | past | comments | ask | show | jobs | submit login

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]);



Also using underscore, with sortBy:

    _.chain(people)
    .sortBy(function(person) { 
      return person.firstName.length + person.lastName.length; })
    .last()
    .value()


FYI, 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;
  });




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: