>我正在使用以下代码生成一个 int 数组,
this.values = new Array(3).fill(0).map((v, i) => i + 1);
然后当我这样做时,
this.quantity = Math.max(this.values);
它返回 NAN。 问题出在哪里? 当我放置一个控制台.log(this.values(时,我可以看到以[1,2,3]作为值的Interger数组。
Math.max
不需要数组,而是需要多个参数。(您的数组被强制转换为一个数字,这会导致NaN
,因为"1,2,3"
不是数字(。
您可以对调用使用展开语法:
this.quantity = Math.max(...this.values);
函数Math.max()
不是那样工作的。它接受两个(或更多(整数,然后返回较大的整数。例如:
Math.max(3, 1, 2); // returns 3
数学.max不带数组,试试这个
Math.max.apply(Math, this.values);