JavaScript 中动态类型数组中的最大数字
javascriptweb developmentfront end technologyobject oriented programming更新于 2026/2/9 9:48:17
我们需要编写一个 JavaScript 函数,该函数接受一个包含一些数字、一些字符串和一些假值的数组。我们的函数应该返回数组中的最大数字。
例如:如果输入数组是 −
const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii'];
那么输出应该是 65。
因此,让我们编写该函数的代码 −
示例
其代码为 −
const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii'];
const pickBiggest = arr => {
let max = -Infinity;
for(let i = 0; i < arr.length; i++){
if(!+arr[i]){
continue;
};
max = Math.max(max, +arr[i]);
};
return max;
};
console.log(pickBiggest(arr));
输出
控制台中的输出将是 −
65
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

