求数组的最大值和最小值
Hkc 2018-03-05 js
# 循环遍历
var arr = [6, 4, 1, 8, 2, 11, 23]
var result = arr[0]
for (var i = 1; i < arr.length; i++) {
result = Math.max(result, arr[i])
}
console.log(result)
# 排序
var arr = [6, 4, 1, 8, 2, 11, 23]
arr.sort(function(a, b) {
return a - b
})
console.log(arr[arr.length - 1])
# apply
var arr = [6, 4, 1, 8, 2, 11, 23]
console.log(Math.max.apply(null, arr))
# es6
var arr = [6, 4, 1, 8, 2, 11, 23]
console.log(Math.max(...arr))
//等价于apply