Javascript求数组的最大值

1.reduce

let arr=[1,4,5,6,20,10]
let max=arr.reduce((pre,cur)=>Math.max(pre,cur),-Infinity)
console.log(max) //20

 2. ...(es6展开符)

let arr1=[1,4,5,6,20,10]
let max1=Math.max(...arr1)
console.log(max1) //20

3.apply

let arr2=[1,4,5,6,20,10]

let max2=Math.max.apply(null, arr2)

console.log(max2) //20

猜你喜欢

转载自blog.csdn.net/qq_38902432/article/details/130895371