Javascript 为什么这个计算返回无穷大?

Javascript 为什么这个计算返回无穷大?,javascript,node.js,Javascript,Node.js,所以我需要在我的脚本中做一些简单的减法运算,但是它并不像原来那么简单,我做错了什么 x= dbo.collection("master").find({Process: "Process1"}).toArray(); //Query my Mongo database s1 = Math.max.apply(null, x.map(o => o.NumberInt)); //narrow down query to find the correct number s2 = (s1-1);

所以我需要在我的脚本中做一些简单的减法运算,但是它并不像原来那么简单,我做错了什么

x= dbo.collection("master").find({Process: "Process1"}).toArray(); //Query my Mongo database
s1 = Math.max.apply(null, x.map(o => o.NumberInt)); //narrow down query to find the correct number 
s2 = (s1-1); //minus 1 from the result to find next result
//s2 returns -Infinity

我100%确信
s1
是一个整数

如果您提供的参数不是数字(或不能强制为数字),那么结果将是
NaN

Math.max
在没有任何参数的情况下被调用时,只会返回
-Infinity

console.log(Math.max());//-无穷
console.log(Math.max(1));//1.
console.log(Math.max(“”);//0(强制为0)
console.log(Math.max({}));//NaN(不是数字)
为什么这个计算返回无穷大

检查
Math.max.apply(null,输入)
可以返回
Infinity
-Infinity

函数doTheMagic(输入){
返回Math.max.apply(null,输入);
}
console.log(doTheMagic([1,2]);
日志(doTheMagic([“1”,“2”]);
log(doTheMagic([null,未定义]);
log(doTheMagic([Infinity]);
log(doTheMagic(null));
log(doTheMagic({}));

console.log(doTheMagic([])
您是否尝试过
console.log(s1)
对其进行双重检查?也可能是
console.log(x.length)
。如果你调用没有参数的
Math.max
(如果
x
是长度0),你会得到
-Infinity
。如果
s1
肯定是一个整数,那么它是什么整数?如果你把它记录下来,你就会知道答案。如果
s1
是2495,那么s2就是2494。你的电脑可能没有坏。人们建议您添加
console.log()
语句来验证您认为正确的内容。显示的代码和
s1
的值为2495,因此
s2
不可能是
-无穷大
。有些事情不像你描述的那样!我认为你是对的,这只是代码的一小部分……我认为我的问题要广泛得多。由于JS的同步特性,它在我的数据库更新之前运行这部分代码(因此此时数据库是空白的)。干杯anyway@PatrickRoberts是的,忘记了
函数#apply
只接受可迭代对象。更新了答案。