Find the min/max element of an Array in JavaScript

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

    The following code works for me :

    var valueList = [10,4,17,9,3];
    var maxValue = valueList.reduce(function(a, b) { return Math.max(a, b); });
    var minValue = valueList.reduce(function(a, b) { return Math.min(a, b); });
    

提交回复
热议问题