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

后端 未结 9 1465
再見小時候
再見小時候 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条回答
  •  天涯浪人
    2021-01-13 04:33

    for big 2D arrays use this avoid function.prototype.apply as in above almost all answers i can see it. because it can handle only limited amount of array length .

    function MaxMin2dray(arr, idx){
        var max = Number.MIN_VALUE;
        var min = Number.MAX_VALUE;
         arr.forEach(function(e) {
         if (max < e[idx]) {
          max = e[idx];
        }
        if (min > e[idx]) {
          min = e[idx];
        }
      });
      return {max: max, min: min};
    }
    

提交回复
热议问题