javascript相当于数组的php max

I have a function in php that selects the array with that contains the most elements.

$firstArray = array('firstArray','blah','blah','blah');
$secondArray = array('secondArray','blah','blah');
$thirdArray = array('thirdArray','blah','blah','blah','blah');

then I get the name of the variable with the highest length like this:

$highest = max($firstArray, $secondArray, $thirdArray)[0];

but I am developing an application and I want to avoid using php and I have tried javascript's Math.max() to achieve the same results but it doesn't work the same way unless I do

Math.max(firstArray.length, secondArray.length, thirdArray.length)

But this is useless since I need to know the name of the array that contains the most elements. Is there any other way to achieve this?

This function takes as input an array of arrays, and returns the largest one.

function largestArray(arrays){
   var largest;
   for(var i = 0; i < arrays.length; i++){
       if(!largest || arrays[i].length > largest.length){
          largest = arrays[i];
       }
   }
   return largest;
}

We can test it out with your example:

firstArray = ['firstArray','blah','blah','blah'];
secondArray = ['secondArray','blah','blah'];
thirdArray = ['thirdArray','blah','blah','blah','blah'];

// should print the third array
console.log(largestArray([firstArray, secondArray, thirdArray]));

The following url has a max() equivalent. It supports more then just numbers just like in php:

js max equivalent of php

If you feel ok with including 3rd-party libs, maybe http://underscorejs.org/#max does what you want:

var aList = [firstArray, secondArray, thirdArray];
_.max(aList, function(each) { return each.length; });