js 数组取最大值方法有哪些
- Math.max()
let arr = [3,1,2,4,6,0,19];
console.log(Math.max(...arr)) // 19
function mathGetMax(arr){
return Math.max.apply(null,arr)//19
}
- sort
const arr = [-1,0,-2,2,3,-3,10,100];
function sortGetMax(arr){
return arr.sort((a,b)=>b-a)[0];//arr.sort((a,b)=>b-a)//降序[100, 10, 3, 2, 0, -1, -2, -3]
}
sortGetMax(arr);//100
- reduce
let arr = [3,1,2,4,6,0,19];
function reduceGetMax(arr){
return arr.reduce((a,b)=>{
return a>b?a:b
})
}
reduceGetMax(arr) // 19
标签:sort,function,arr,return,--,js,19,中取,Math
From: https://www.cnblogs.com/zhu-xl/p/17167572.html