Max value of a multidimensional array javascript

前端 未结 7 2307
太阳男子
太阳男子 2021-02-09 18:35

I have a multidimensional array of x columns and y rows. How can I find the min and the max value of the matrix? Example:

[[1,  37.8, 80.8, 41.8],
[2,  30.9, 69.         


        
相关标签:
7条回答
  • 2021-02-09 19:37

    Most answers here use apply or the spread operator ... to call the Math.max function with all the elements of an array as parameters.

    For large arrays it is safer to use reduce:

    // returns maximum of an array
    const getArrayMax = array => array.reduce((a, b) => Math.max(a, b));
    
    // returns maximum of a 2D array
    const getArrayMax2d = array2d => getArrayMax(array2d.map(getArrayMax));
    
    0 讨论(0)
提交回复
热议问题