If you have an array containing an indefinite amount of arrays
ex:
var masterArray = [ [1,2,3,4,5],
[1,2],
[1,1,
You can iterate over all entries of the outer array using a for
loop and compare the length of each of its items to the longest array you have found so far.
The following function returns the index of the longest array or -1
if the array is empty.
function indexOfLongest(arrays) {
var longest = -1;
for (var i = 0; i < arrays.length; i++) {
if (longest == -1 || arrays[i].length > arrays[longest].length) {
longest = i;
}
}
return longest;
}
var masterArray = [ [1,2,3,4,5],
[1,2],
[1,1,1,1,2,2,2,2,4,4],
[1,2,3,4,5] ];
document.write(indexOfLongest(masterArray));