Find the min/max element of an Array in JavaScript

前端 未结 30 2163
無奈伤痛
無奈伤痛 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 07:00

    One more way to do it:

    var arrayMax = Function.prototype.apply.bind(Math.max, null);
    

    Usage:

    var max = arrayMax([2, 5, 1]);
    

提交回复
热议问题