【问题标题】:Understanding Math.max() [duplicate]理解 Math.max() [重复]
【发布时间】:2021-07-29 13:28:38
【问题描述】:

所以我有四个不同长度的数组。我会称它们为一、二、三、四。

为了找到最长的,我把它们放在一个数组中。

let length = [one.length, two.length, three.length, four.length];

所以我得到类似length = [10,54,22,4];

Math.max 作品如果我这样做了Math.max(...length);

但我不明白为什么 Math.max([one.length,two.length,three.length,four.length]) returns NaN.

【问题讨论】:

标签: javascript arrays math methods


【解决方案1】:

仅仅是因为Math.max() 试图将其参数转换为数字。数组不会转换成数字,所以会得到 NaN。

调用Math.max(...lengths)lengths 数组拆分为几个参数,这些参数有效(当然,只要它们都是数字)。

【讨论】:

  • 所以 Math.max 试图找到最大数,但数组本身尚未通过 .length 方法转换?所以不是 Math.max([10,12,30,45]) 它类似于 Math.max([1,2,3], [21,7,9], [54, 60,11], [24 ,5,63]) 它不能做 b/c 它们仍然是数组而不是数字?
猜你喜欢
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多