Max value of a multidimensional array javascript

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

    You can get max value of multi dimension array using following method:

    var arr = [[1, 5,6], [4, 7,8], [3, 8,20], [2, 3,1],[12, 4,5]];
    
    console.log(Math.max.apply(Math, arr.map(function (i) {
        return i[0]+i[1]+i[2];
    })));
    

    It first use array.map() to convert the multi-dimensional array to a flat one, and after that use Math.max().

提交回复
热议问题