获取数组中的最大值(最小值)

2019-11-22  本文已影响0人  Leonard被注册了

如题所示,开始敲代码

let ary = [12, 13, 14, 23, 24, 13, 15, 12];
let max = ary.sort((a, b) => b - a)[0];
let min = ary.sort((a, b) => b - a)[ary.length - 1];
console.log(max, min);   // 24 12
let ary = [12, 13, 14, 23, 24, 13, 15, 12];
let max = ary[0],
    min = ary[0];
for (let i = 1; i < ary.length; i++) {
    let item = ary[i];
    item > max ? max = item : null;
    item < min ? min = item : null;
}
console.log(max, min);   // 24 12
let ary = [12, 13, 14, 23, 24, 13, 15, 12];
let max = Math.max.apply(null,ary);
let min = Math.min.apply(null,ary)
console.log(max, min);   // 24 12
let ary = [12, 13, 14, 23, 24, 13, 15, 12];
let max = eval("Math.max("+ary.join()+")");
let min = eval("Math.min("+ary.join()+")");
console.log(max, min);   // 24 12
let ary = [12, 13, 14, 23, 24, 13, 15, 12];
let max = Math.max(...ary);
let min = Math.min(...ary)
console.log(max, min);   // 24 12
上一篇下一篇

猜你喜欢

热点阅读