Get the maximum length of a javascript array element

If I have the following array:

array = ['a', 'abcde', 'ab'] ; 

I would like to get the maximum length of the elements of the array, i.e. 5 (for the element 'abcde').

I know that you can get the length of an array element through (for example) the 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

+9
javascript jquery arrays
source share
8 answers

One line for you:

  Math.max.apply(Math, $.map(array, function (el) { return el.length })); 

Working example: http://jsfiddle.net/5SDBx/

You can do this without jQuery in new browsers (or even older browsers with Array.prototype.map compatibility Array.prototype.map ) too:

 Math.max.apply(Math, array.map(function (el) { return el.length })); 
+21
source share

One (possibly garbage) way to do this 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; } } 
+5
source share

New answer to the old question: in ES6 you can do even shorter:

 Math.max(...array.map(el => el.length)); 
+3
source share

Using Array.prototype.reduce() , you can elegantly calculate Math.max() for each element of the array. It doesn't matter whether the elements of the array are string or subarrays (2D array, matrix, etc.), This will work because String and Array have prototype.length() function.

I also added integer checking to 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);} 
+2
source share

You can use jQuery each() to play around a bit.

 var maxLength = 0; $.each(array, function(i, item) { maxLength = Math.max(maxLength, item.length); }); 

Or simple ol 'javascript ...

 var maxLength = 0; for (var i = 0, length = array.length; i < length; i++) { maxLength = Math.max(maxLength, array[i].length); }; 
+1
source share

There are several ways to do this.

I would iterate over all the elements of the array and store the longest element in a local variable. You get the longest element by comparing the first element of the array with the second, and then preserving the longer. Then go ahead and compare each element with the longest.

For this you need only one cycle.

0
source share

You can create your own code. Follow this โ†’

 Array.prototype.count = function(){ return this.length; } 

then use

 arr = [ "erhan",1,"sonmez",2 ]; arrCount = arr.count(); 
0
source share

In such cases, there is a very useful feature. Array.reduce

 ['a', 'abcde', 'ab'].reduce((r,s) => r.length > s.length ? r : s, 0); 
0
source share

All Articles