Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我在javascript中有一个低温数组,我需要得到最低温度,所以我使用了min.math函数_Javascript_Function_Min - Fatal编程技术网

我在javascript中有一个低温数组,我需要得到最低温度,所以我使用了min.math函数

我在javascript中有一个低温数组,我需要得到最低温度,所以我使用了min.math函数,javascript,function,min,Javascript,Function,Min,我在javascript中有一个低温数组,我需要得到最低温度,所以我使用了math.min函数。第一次虽然低温被返回,然后我得到了NaN var lowTempArray = []; var lowest = Math.min(lowTempArray); message += '<tr><td colspan="3"class="alt">The lowest temperature of ' + lowest.toFixed(2) + ' occurred on

我在javascript中有一个低温数组,我需要得到最低温度,所以我使用了math.min函数。第一次虽然低温被返回,然后我得到了NaN

var lowTempArray = []; 
var lowest = Math.min(lowTempArray);

message += '<tr><td colspan="3"class="alt">The lowest temperature of ' + lowest.toFixed(2) + ' occurred on ' + ((date.getMonth() + 1) + '/' + (date.getDate()) + '/' +  date.getFullYear()) + '.' + '</td></tr>';
var-lowTempArray=[];
var lowest=数学最小值(lowTempArray);
消息+='最低温度'+lowest.toFixed(2)+'发生在'+((date.getMonth()+1)+'/'+(date.getDate())+'/'+date.getFullYear())+'.+'''.+';

Math.min
将需要一系列数字,如
Math.min(1,2,3,4,5)
,而不是数组

您必须手动遍历数组,如下所示:

var lowest = Number.MAX_VALUE;
for ( var i = 0; i < lowTempArray.length; i++ ) {
    if ( lowTempArray[i] < lowest ) {
        lowest = lowTempArray[i];
    }
}
var lowest = lowest( lowTempArray );

正如其他人所建议的那样,排序也是一种方法,但在处理一个长列表时可能需要更多的CPU资源。

由于
Math.min
只需要一系列数字,我建议只对数组进行排序,然后使用第一个元素

所以你可以

lowTempArray.sort(function(a,b){return a-b;});
var lowest = lowTempArray[0];
然后在其他语句中使用
lower.toFixed(2)


干杯:)

由于
min
需要参数列表,而不是数组,因此可以使用
apply
将数组用作函数的参数列表,例如:

var arr=[4,5,6,3,1,7,8];
Math.min.apply(this,arr); //1
请参阅有关应用的更多信息:

是否有一个函数可用于从数组中获取最小值?这个数组是通过用户的输入创建的。我上面写的代码将在变量
lowTempArray
中给set一个最低值。是的,for循环是一个很好的选择,关于排序和CPU压力,你可能是对的。这是一个非常简单的web搜索,非常感谢!真是太棒了!嘿,你知道如何获取输入的日期吗?我想我需要对低温下的项使用indexOf,然后为date对象调用相同的索引号。我可能完全错了。哦,这个数字是作为字符串返回的,所以我不得不使用parselnt:lowest=parseInt(lowest,10);我不知道你对日期有什么要求。这可能是另一个问题。至少你需要详细说明一下,所以我会问一个新问题。谢谢
var arr=[4,5,6,3,1,7,8];
Math.min.apply(this,arr); //1