Find the min/max element of an Array in JavaScript

前端 未结 30 2066
無奈伤痛
無奈伤痛 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: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.

提交回复
热议问题