- 将数组排序:
var arr = [32,1,434,4,3,21,-1]
arr.sort((a,b)=>a-b) // [-1, 1, 3, 4, 21, 32, 434]
arr[0] // -1
arr[arr.lenght -1] // 434
- Math.min() 和 Math.max()
var arr = [32,1,434,4,3,21,-1]
Math.min(...arr) // -1
Math.max(...arr) // 434
- 字符串拼接:
var arr = [32,1,434,4,3,21,-1]
var max = eval("Math.max(" + arr.toString() + ")");
max // 434
- apply 传参:
var arr = [32,1,434,4,3,21,-1]
var max = Math.max.apply(null, arr)
- 假设当前数组中第一个值为最大值,然后拿这个值和后续的值比较,如果后面的值比假设的大,把假设值替换:
var arr = [32,1,434,4,3,21,-1]
var max = arr[0] , min = arr[0];
for (var i =1; i< arr.length; i++){
var cur = arr[i];
cur > max ? max = cur : null;
cur < min ? min = cur : null;
}
console.log(min, max)