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

后端 未结 9 1404
再見小時候
再見小時候 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};
    }
    
    0 讨论(0)
  • 2021-01-13 04:35

    You can map the array to the second values of the elements:

    var arr = [[[1, 112.0],[2,5.12],[3,113.1],[4,33.6],[5,85.9],[6,219.9]]];
    var values = arr[0].map(function(elt) { return elt[1]; });
    var max = Math.max.apply(null, values);
    var min = Math.min.apply(null, values);
    
    0 讨论(0)
  • 2021-01-13 04:36

    I really admired the sleek readability of @TedHopp 's solution -- so much so that I decided to adapt it into a function. The practicality of the function is still yet to be seen, but I liked the idea of being able to identify the min / max of a specified index within a 2D array.

    function getMinMaxOf2DIndex (arr, idx) {
        return {
            min: Math.min.apply(null, arr.map(function (e) { return e[idx]})),
            max: Math.max.apply(null, arr.map(function (e) { return e[idx]}))
        }
    } 
    

    getMinMaxOf2DIndex() takes two parameters, arr -- an array, and idx the index of the values to compare.

    Usage Examples:

    // using the OP's array as an example
    var array = [[1, 112.0],[2,5.12],[3,113.1],[4,33.6],[5,85.9],[6,219.9]];
    
    getMinMaxOf2DIndex(array, 0); // {min: 1, max: 6}
    getMinMaxOf2DIndex(array, 1); // {min: 5.12, max: 219.9}
    
    // and so on...
    var array = [[1, 9, 6 , 3], [2, 4, 7, 2], [6, 5, 9, 4]];
    getMinMaxOf2DIndex(array, 2); // {min: 1, max: 6}
    getMinMaxOf2DIndex(array, 3); // {min: 6, max: 9}
    
    0 讨论(0)
  • 2021-01-13 04:36

    You can loop through the first array (the only one here) with a forEach:

    var A= [
        [[1, 112.0], [2, 5.12], [3, 113.1], [4, 33.6], [5, 85.9], [6, 219.9]]
    ];
    var mm= [Infinity, -Infinity];
    
    A[0].forEach(function(itm){
        var n= itm[1];
        if(n<mm[0]) mm[0]= n;
        if(n>mm[1]) mm[1]= n;   
    });
    

    mm

    /* returned value: (Array) [5.12, 219.9] */

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2021-01-13 04:38

    In ES6 you can use spread operator:

    var max = Math.max(...arrayOfValues);
    

    Working example:

    var arr = [[[1, 112.0],[2,5.12],[3,113.1],[4,33.6],[5,85.9],[6,219.9]]];
    
    var v = arr[0].map(e => e[1]);
    
    var max = Math.max(...v);
    var min = Math.min(...v);
    
    console.log('max: ' + max);
    console.log('min: ' + min);

    0 讨论(0)
提交回复
热议问题