最值

常见的使用方法就是基础库提供的max和min。

一、使用排序

  1. 获取最大值:

    const arr = [5, 3, 8, 1, 6];
    arr.sort((a, b) => b - a);
    const maxValue = arr[0];
    console.log(maxValue); // 8
  2. 获取最小值:

    const arr = [5, 3, 8, 1, 6];
    arr.sort((a, b) => a - b);
    const minValue = arr[0];
    console.log(minValue); // 1

二、使用扩展运算符和Math方法

  1. 获取最大值:

    const arr = [5, 3, 8, 1, 6];
    const maxValue = Math.max(...arr);
    console.log(maxValue); // 8
  2. 获取最小值:

    const arr = [5, 3, 8, 1, 6];
    const minValue = Math.min(...arr);
    console.log(minValue); // 1

三、遍历数组

  1. 获取最大值:

  2. 获取最小值:

Last updated