当前位置:网站首页>Math. Max() method obtains the maximum value in the array and returns Nan problem analysis
Math. Max() method obtains the maximum value in the array and returns Nan problem analysis
2022-06-23 23:39:00 【Deshun】
Today, someone in the group asked Math.max() Method returns NaN The problem of , Let me give you a simple example , Look at the picture below :
It looks fine , But why return NaN Well ? Let's take a brief look at Math.max() Method :
Math.max()
Math.max() Function returns the maximum value of a set of numbers .
Demo:
console.log(Math.max(1, 3, 2)); // expected output: 3 console.log(Math.max(-1, -3, -2)); // expected output: -1 const array1 = [1, 3, 2]; console.log(Math.max(...array1)); // expected output: 3
grammar :
Math.max(value1[,value2, ...])
Parameters : A set of values
value1, value2, ...
Return value :
Returns the maximum of a given set of numbers .
Be careful : If at least one of the given parameters cannot be converted to a number , Will return NaN.
Problem solving
Careful observation shows that , The code uses ... deconstruction , No problem ,ES6 The syntax supports this , Will solve the array into a set of values .
But the problem here is array Is a two-dimensional array , Deconstructed or an array , Not numbers , So back NaN 了 .
besides , There will also be some scenes NaN , A simple example :
var arr=[1,2,3,45,66]
var num =Math.max(arr.join(','))
alert(num) If you write like this , use arr.join(',') What you get is a string , It is equivalent to a string :
Math.max( '1,2,3,45,66' );
resolvent :
var arr = [1,2,3,45,66] var num = Math.max.apply( null, arr ); console.log( num );
apply The second parameter to is the parameter array .
perhaps :
var arr = [1,2,3,45,66]
var num = eval( 'Math.max(' + arr.join( ',' ) + ')' );
console.log( num ); // 66 If you insist on splicing parameters with strings , It can be used eval Method .
No reprint without permission :w3h5 » Math.max() Method to get the maximum value in the array and return NaN Problem analysis
边栏推荐
- Generate post order traversal according to pre order traversal and mid order traversal
- PLC数据操作系列之构造不同应用场景的缓存栈FIFO(算法详解)
- 6. STM32 - serial port data transceiver Foundation
- E: 无法获得锁 /var/lib/dpkg/lock
- Solve the problem that slf4j logs are not printed
- “山大地纬杯”第十二届山东省ICPC大学生程序设计竞赛
- Develop synergy and efficiently manage | community essay solicitation
- Summary of some indicators for evaluating and selecting the best learning model
- 在OpenCloudOS使用snap安装.NET 6
- [design] 1359- how umi3 implements plug-in architecture
猜你喜欢

ACM. HJ89 24点运算 ●●●
Summary of cloud native pipeline tools

微信视频号如何用 PC 电脑做直播?

谈谈数字化转型晓知识

List中subList的add造成的死循环
![[Xilinx ax7103 microbalze Learning Notes 6] MicroBlaze custom IP core packaging experiment](/img/26/c4e6be2e3b17c7925108e95943c558.png)
[Xilinx ax7103 microbalze Learning Notes 6] MicroBlaze custom IP core packaging experiment

嵌入式接口之TIM定时器与NVIC的STM32模板库函数的一些解释

解决Slf4j日志不打印问题

Cause analysis and Countermeasures for FANUC robot srvo-050 collision detection alarm (available for personal test)

Cs5213 HDMI to VGA with audio signal output scheme
随机推荐
STM32-------外部中断
“山大地纬杯”第十二届山东省ICPC大学生程序设计竞赛
PyQt5_QTableWidget分页单选右键菜单控件
Some explanations of Tim timer of embedded interface and STM32 template library function of NVIC
2022年信息安全工程師考試知識點:訪問控制
PLC数据操作系列之构造不同应用场景的缓存栈FIFO(算法详解)
2022 point de connaissance de l'examen des ingénieurs en sécurité de l'information: contrôle d'accès
国内外最好的12款项目管理系统优劣势分析
MySQL transaction isolation
laravel之任务队列
Analysis of Alibaba cloud Tianchi competition -- prediction of o2o coupon
The Sandbox 归属周来啦!
E: Unable to acquire lock /var/lib/dpkg/lock
Stm32-------adc (voltage detection)
What is the production process of enterprise website? How long does it take to design and build a website?
ORB_SLAM3环境搭建及demo演示
iNFTnews | 创造者经济的未来在Web3世界中该去向何处?
Task queue of laravel
"Shanda Diwei Cup" the 12th Shandong ICPC undergraduate program design competition
What is an immunohistochemical experiment? Immunohistochemical experiment