Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get maximum length of JavaScript array element

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

like image 774
Paul Jones Avatar asked May 18 '11 10:05

Paul Jones


People also ask

What is the maximum length of an array in JavaScript?

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.

How do I get the size of an array in JavaScript?

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.

How do you find the largest element in an array?

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.


6 Answers

A new answer to an old question: in ES6 you can do even shorter:

Math.max(...array.map(el => el.length));
like image 57
Dávid Veszelovszki Avatar answered Oct 21 '22 13:10

Dávid Veszelovszki


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 }));
like image 26
Andy E Avatar answered Oct 21 '22 11:10

Andy E


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;
  }
}
like image 27
James Avatar answered Oct 21 '22 13:10

James


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);
};
like image 42
alex Avatar answered Oct 21 '22 12:10

alex


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);}
like image 23
Mr. Polywhirl Avatar answered Oct 21 '22 11:10

Mr. Polywhirl


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

['a', 'abcde', 'ab'].reduce((r,s) => r.length > s.length ? r : s, 0);
like image 27
Manvel Avatar answered Oct 21 '22 11:10

Manvel