Getting the min and max value in JavaScript, but from a 2D array

后端 未结 9 1470
再見小時候
再見小時候 2021-01-13 04:32

I know this gets asked again and again but hear me out - this question is slightly different.

I can get a max or min from a 1D array like this:

var          


        
9条回答
  •  Happy的楠姐
    2021-01-13 04:37

    That array you have in the post is interesting, but this works for it.

    var array = [[[1, 112.0],[2,5.12],[3,113.1],[4,33.6],[5,85.9],[6,219.9]]];
    function getMax(a){
        var max = a[0][0][1];
        for(var i = 0; i< a[0].length; i++){
            if(a[0][i][1] > max)
                max = a[0][i][1];
        }
        return max;
    }
    

提交回复
热议问题