// 使用 Math.max() 方法取最大值
function getMaxValue(arr) {
if (arr.length === 0) {
return null; // 如果数组为空,返回 null
}
return Math.max(...arr); // 使用扩展运算符将数组元素传递给 Math.max()
}
// 示例
const numbers = [1, 5, 2, 9, 3];
const maxValue = getMaxValue(numbers);
console.log(maxValue); // 输出: 9
Math.max() 是 JavaScript 中用于获取一组数值中最大值的内置函数。getMaxValue 函数,它接受一个数组作为参数,并使用扩展运算符 (...) 将数组中的元素传递给 Math.max() 函数。null,以避免调用 Math.max() 时返回 -Infinity。[1, 5, 2, 9, 3] 中的最大值。上一篇:js 前端分页
下一篇:js if判断多个条件
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站