5-1reduce()归并求和

  1. var arr=[1,2,3,4];
  2. var sum=arr.reduce((a,b)=>{
  3. return a+b;
  4. })
  5. console.log(sum);//10
  6. console.log(arr);//[1,2,3,4]

5-2Math max()取最大值

  1. var arr=[1,2,3];
  2. console.log(Math.max(12,3,4));//12
  3. console.log(Math.max(...arr));//要先展开数组,才可以找最大值 3

5-3Math.min()取最小值

  1. var arr=[5,4,3,2];
  2. console.log(Math.min(...arr));
  3. //算法
  4. var arr=[5,4,3,2];
  5. var minNum=arr[0]
  6. for(var i=1;i<arr.length;i++){
  7. if(minNum>arr[i]){
  8. minNum=arr[i]
  9. }
  10. }
  11. console.log(minNum);