If I have the following array :
array = ['a', 'abcde', 'ab'] ;
I would like to get the maximum length of the array elements ie 5 (for the element 'abcde').
I know that you can get the length of an array element via (eg) array[1].length but I don't know how to get the maximum length.
Any answer in JavaScript or jQuery would be great.
TIA
Paul Jones
JavaScript arrays are zero-based and use 32-bit indexes: the index of the first element is 0, and the highest possible index is 4294967294 (232−2), for a maximum array size of 4,294,967,295 elements.
The JavaScript array length property states the number of items in an array. To find the length of an array, reference the object array_name. length. The length property returns an integer.
To find the largest element from the array, a simple way is to arrange the elements in ascending order. After sorting, the first element will represent the smallest element, the next element will be the second smallest, and going on, the last element will be the largest element of the array.
A new answer to an old question: in ES6 you can do even shorter:
Math.max(...array.map(el => el.length));
One-liner for you:
Math.max.apply(Math, $.map(array, function (el) { return el.length }));
Working example: http://jsfiddle.net/5SDBx/
You can do it without jQuery in newer browsers (or even older browsers with a compatibility implementation of Array.prototype.map
) too:
Math.max.apply(Math, array.map(function (el) { return el.length }));
One (possibly rubbish) way of doing that, without using Jquery:
var myarray = ['a','abcde','ab'];
var maxlen = 0;
for (i=0; i<myarray.length; i++) {
if (myarray[i].length>maxlen) {
maxlen = myarray[i].length;
}
}
You could use jQuery's each()
to iterate a little nicer.
var maxLength = 0;
$.each(array, function(i, item) {
maxLength = Math.max(maxLength, item.length);
});
Or plain ol' JavaScript...
var maxLength = 0;
for (var i = 0, length = array.length; i < length; i++) {
maxLength = Math.max(maxLength, array[i].length);
};
By using Array.prototype.reduce()
, you can elegantly perform a Math.max()
calculation for each item in the array. It does not matter whether the items in the array are string or sub-arrays (2D array, matrix, etc.), this will work because String
and Array
both have a prototype.length()
function.
I also added a check for integer values per your comment above.
function arrayItemMax(arr) {
return arr.reduce(function (result, val) {
return Math.max(result, !isNaN(val) ? parseInt(val) : val.length);
}, 0);
}
function print(text) {
document.getElementsByTagName('body')[0].innerHTML += text + '<br />';
}
print(arrayItemMax(['a', 'abcde', 'ab'])); // 5
print(arrayItemMax([1, 5, 2])); // 5
print(arrayItemMax(['1', '5', '2'])); // 5
One liner:
function(a){return a.reduce(function(r,v){return Math.max(r,!isNaN(v)?parseInt(v):v.length);},0);}
In such cases, there is a very useful function. Array.reduce
['a', 'abcde', 'ab'].reduce((r,s) => r.length > s.length ? r : s, 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With