Find the index of the longest array in an array of arrays

后端 未结 8 2233
醉话见心
醉话见心 2021-02-05 09:44

If you have an array containing an indefinite amount of arrays

ex:

var masterArray = [ [1,2,3,4,5],
                    [1,2], 
                    [1,1,         


        
8条回答
  •  忘掉有多难
    2021-02-05 10:16

    Using lodash:

    _.max(_.map(masterArray, function(v, k) { return { id: k, size: v.length }; }),'size').id;
    

    This creates a new array with objects having 'id' and 'size', then finds the maximum size in that array, and returns its 'id'.

    jsfiddle: https://jsfiddle.net/mckinleymedia/8xo5ywbc/

提交回复
热议问题