如何找到 JavaScript 数组中包含的最大数?

2022-01-31 00:00:00 algorithm arrays max javascript

我有一个包含几个数字的简单 JavaScript 数组对象.

I have a simple JavaScript Array object containing a few numbers.

[267, 306, 108]

有没有一个函数可以在这个数组中找到最大的数?

Is there a function that would find the largest number in this array?

推荐答案

请求救援:

Array.max = function( array ){
    return Math.max.apply( Math, array );
};

警告:因为 在某些虚拟机上,参数的最大数量低至 65535,如果您不确定数组有那么小,请使用 for 循环.

Warning: since the maximum number of arguments is as low as 65535 on some VMs, use a for loop if you're not certain the array is that small.

相关文章