Find the min/max element of an Array in JavaScript

前端 未结 30 2045
無奈伤痛
無奈伤痛 2020-11-21 06:18

How can I easily obtain the min or max element of a JavaScript Array?

Example Psuedocode:

let array = [100, 0, 50]

array.min() //=> 0
array.max()         


        
相关标签:
30条回答
  • 2020-11-21 06:54

    How about augmenting the built-in Array object to use Math.max/Math.min instead:

    Array.prototype.max = function() {
      return Math.max.apply(null, this);
    };
    
    Array.prototype.min = function() {
      return Math.min.apply(null, this);
    };
    

    Here is a JSFiddle.

    Augmenting the built-ins can cause collisions with other libraries (some see), so you may be more comfortable with just apply'ing Math.xxx() to your array directly:

    var min = Math.min.apply(null, arr),
        max = Math.max.apply(null, arr);
    

    Alternately, assuming your browser supports ECMAScript 6, you can use the spread operator which functions similarly to the apply method:

    var min = Math.min( ...arr ),
        max = Math.max( ...arr );
    
    0 讨论(0)
  • 2020-11-21 06:54

    You can use the following function anywhere in your project:

    function getMin(array){
        return Math.min.apply(Math,array);
    }
    
    function getMax(array){
        return Math.max.apply(Math,array);
    }
    

    And then you can call the functions passing the array:

    var myArray = [1,2,3,4,5,6,7];
    var maximo = getMax(myArray); //return the highest number
    
    0 讨论(0)
  • 2020-11-21 06:55

    Others have already given some solutions in which they augment Array.prototype. All I want in this answer is to clarify whether it should be Math.min.apply( Math, array ) or Math.min.apply( null, array ). So what context should be used, Math or null?

    When passing null as a context to apply, then the context will default to the global object (the window object in the case of browsers). Passing the Math object as the context would be the correct solution, but it won't hurt passing null either. Here's an example when null might cause trouble, when decorating the Math.max function:

    // decorate Math.max
    (function (oldMax) {
        Math.max = function () {
            this.foo(); // call Math.foo, or at least that's what we want
    
            return oldMax.apply(this, arguments);
        };
    })(Math.max);
    
    Math.foo = function () {
        print("foo");
    };
    
    Array.prototype.max = function() {
      return Math.max.apply(null, this); // <-- passing null as the context
    };
    
    var max = [1, 2, 3].max();
    
    print(max);
    

    The above will throw an exception because this.foo will be evaluated as window.foo, which is undefined. If we replace null with Math, things will work as expected and the string "foo" will be printed to the screen (I tested this using Mozilla Rhino).

    You can pretty much assume that nobody has decorated Math.max so, passing null will work without problems.

    0 讨论(0)
  • 2020-11-21 06:56

    https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Math/max

    function getMaxOfArray(numArray) {
      return Math.max.apply(null, numArray);
    }
    
    var arr = [100, 0, 50];
    console.log(getMaxOfArray(arr))

    this worked for me.

    0 讨论(0)
  • 2020-11-21 06:57

    You do it by extending the Array type:

    Array.max = function( array ){
        return Math.max.apply( Math, array );
    };
    Array.min = function( array ){
        return Math.min.apply( Math, array );
    }; 
    

    Boosted from here (by John Resig)

    0 讨论(0)
  • 2020-11-21 06:57

    I thought I'd share my simple and easy to understand solution.

    For the min:

    var arr = [3, 4, 12, 1, 0, 5];
    var min = arr[0];
    for (var k = 1; k < arr.length; k++) {
      if (arr[k] < min) {
        min = arr[k];
      }
    }
    console.log("Min is: " + min);

    And for the max:

    var arr = [3, 4, 12, 1, 0, 5];
    var max = arr[0];
    for (var k = 1; k < arr.length; k++) {
      if (arr[k] > max) {
        max = arr[k];
      }
    }
    console.log("Max is: " + max);

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