Is there a short way to find the longest string in a string array?
Something like arr.Max(x => x.Length);?
arr.Max(x => x.Length);
Available since Javascript 1.8/ECMAScript 5 and available in most older browsers:
var longest = arr.reduce( function (a, b) { return a.length > b.length ? a : b; } );
Otherwise, a safe alternative:
var longest = arr.sort( function (a, b) { return b.length - a.length; } )[0];
2.1m questions
2.1m answers
60 comments
57.0k users