2021-11-05

js中获取数组中最小值

方法如下

var arry = [1, 3, 2, 9, 5, 3, 1, 5, 3, 0, 9, 3, 2, 4, 2, 5, 7];
//方法一:循环遍历
var minNum = arry[0];
arry.forEach((val, index) => {
if (val < arry[index + 1] && val < minNum) {
minNum = val;
}
})
console.log(minNum)
//方法一:循环遍历

//方法二:sort
arry.sort((a,b) => {
    return a-b
})
console.log(arry[0])
//方法二:sort

//方法三:Math.min()
Math.min(...arry)
//方法三:Math.min()

//方法四:reduce()
let arry = [11, 12, 13, 14, 15, 16, 17, 12, 12, 9, 11];
let result = arry.reduce((x, y) => x < y ? x : y)
console.log(result)//9
上一篇:vSphere 6.5中网络感知的DRS解析


下一篇:ECS训练营DAY5——搭建个人Leanote云笔记本