javascript - 数学最小值和最大值返回 NaN
问题描述
我想从一个数字数组中记录最小和最大数字,这些数字在运行时会不断变化。我正在使用 ES5Math.min
和Math.max
. 我什至克隆输入数组以避免并发更改。这是我的代码:
var runTimes = input.runTimes.slice(0); // clone input to avoid further changes
console.log("RUN TIMES", runTimes);
var min = Math.min(runTimes);
var max = Math.max(runTimes);
console.log("RUN TIMES MIN", min);
console.log("RUN TIMES MAX", max);
解决方案
Math.min
或者Math.max
不要期望数组而是不同的数字。请试试这个:
var min = Math.min(...runTimes);
var max = Math.max(...runTimes);
推荐阅读
- svelte - “无法获得 /” - 404
- javascript - Gatsby markdown frontmatter 链接变成 GraphQL 静态查询后的相对文件路径
- python - #rdflib (python):如何从字符串中获取 URIRef,例如 'ns:xxx'?
- rx-java - RxJava Flowable 中的延迟元素
- javascript - 如何使用 core-js 减少向上导入?
- python-3.x - Tkinter - Spinbox - How can I get traced values into a variable?
- mysql - Calculate downtime based on status 'UP' / 'DOWN'
- python - 数据框删除重复值少于 5 个的行
- python-3.x - How to send email with Gmail on a EC2 server
- android-studio - Array that store color maps flutter