Find the min/max element of an Array in JavaScript

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

    Aside using the math function max and min, another function to use is the built in function of sort(): here we go

    const nums = [12, 67, 58, 30].sort((x, y) => 
    x -  y)
    let min_val = nums[0]
    let max_val = nums[nums.length -1]
    

提交回复
热议问题