求数组中的最大值、最小值

求数组中的最大值、最小值

1. ES5写法

// 求数组中的最大值
var a = [1,2,4,3,8,9,12,6];
var max = Math.max.apply(Math,a);
var min = Math.min.apply(Math,a);
console.log(max);	/*12*/
console.log(min);	/*1*/

Math下有max/min方法,是求多个数值中的最大值、最小值。

allpy()方法接收两个参数,第一个:函数运行的环境(this指向),第二个:参数组成的数组

2. ES6写法

// 求数组中的最大值
let a = [1,2,4,3,8,9,12,6];
let max = Math.max(...a);
let min = Math.min(...a);
console.log(max);	/*12*/
console.log(min);	/*1*/

你可能感兴趣的:(前端笔记)