// 使用 Math.max 和 spread 运算符获取数组中的最大值
function getMaxValue(arr) {
if (arr.length === 0) {
return undefined; // 如果数组为空,返回 undefined
}
return Math.max(...arr); // 使用 spread 运算符将数组元素展开为参数传递给 Math.max
}
// 示例
const numbers = [1, 3, 5, 7, 9];
console.log(getMaxValue(numbers)); // 输出: 9
// 对于空数组的情况
const emptyArray = [];
console.log(getMaxValue(emptyArray)); // 输出: undefined
上一篇:js 获取毫秒数
下一篇:js 数组取最大值
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站