(1)for 循环:数组中循环求最大值
var arr=[1,5,9,0,1,-3,-98,100,80];
function fn(){
var max=arr[0];
for(var i=0;i<arr.length;i++){
if(max<arr[i]){
max=arr[i];
}
}return max;
}
console.log(fn());
(2)三目运算配合reduce求取数组当中最大值
console.log(arr.reduce(function(pre,item,index,arr){return pre>item?pre:item;},arr[0]))
(3)sort();方法求取最大值
function fn2(a,b){
return b-a;
}
var newArr=arr.sort(fn2);
//console.log(newArr) 打印排序后的新数组
console.log(newArr.splice(0,1).toString());
(4)循环取数组每一项,拿最大值
var max= arr[0]
for(var x of arr){
max=max>x?max:x;
}
console.log(max);
(5)apply方法解析数组进行传参,求最大值
console.log(Math.max.apply(null,arr));