Max value of a multidimensional array javascript

前端 未结 7 2316
太阳男子
太阳男子 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:32

    based on this answer, you can do it in 1 line (assuming ES6):

    const arr = [[12,45,75], [54,45,2],[23,54,75,2]];
    
    const max = Math.max(...[].concat(...arr));
    
    const min = Math.min(...[].concat(...arr));
    
    console.log(max);
    
    console.log(min);
    

提交回复
热议问题