当前位置:网站首页>js面试收藏试题1
js面试收藏试题1
2022-07-02 04:48:00 【幻彩希】
//求输出结果
console.log(['1','2','3'].map(parseInt))
JavaScript map()函数使用
一、概念
map()
方法定义在JavaScript的Array
中,它返回一个新的数组,数组中的元素为原始数组调用函数处理后的值。
注意:
map()
不会对空数组进行检测map()
不会改变原始数组
二、参数说明:
array.map(function(currentValue, index, arr), thisIndex)
function(currentValue, index, arr)
:必须。为一个函数,数组中的每个元素都会执行这个函数。其中函数参数:
currentValue
:必须。当前元素的的值。index
:可选。当前元素的索引。arr
:可选。当前元素属于的数组对象。
thisValue
:可选。对象作为该执行回调时使用,传递给函数,用作"this
"的值。
所以这道题可以解释为
[
parseInt('1',0),
parseInt('2',1),
parseInt('3',2)
]
再看parseInt()函数的定义
语法
parseInt(string, radix);
parseInt(string, radix) 解析一个字符串并返回指定基数的十进制整数,radix
是 2-36 之间的整数,表示被解析字符串的基数。
参数
string
要被解析的值。如果参数不是一个字符串,则将其转换为字符串 (使用 ToString
抽象操作)。字符串开头的空白符将会被忽略。
radix
可选
从 2
到 36
,表示字符串的基数。例如指定 16
表示被解析值是十六进制数。请注意,10 不是默认值!文章后面的描述解释了当参数 radix
不传时该函数的具体行为。
返回值
从给定的字符串中解析出的一个整数。
或者 NaN,当
radix
小于2
或大于36
,或- 第一个非空格字符不能转换为数字。
如果 radix
是 undefined
、0
或未指定的,JavaScript 会假定以下情况:
- 如果输入的
string
以0x
或0X
(一个 0,后面是小写或大写的 X)开头,那么 radix 被假定为 16,字符串的其余部分被当做十六进制数去解析。 - 如果输入的
string
以 "0
"(0)开头,radix
被假定为8
(八进制)或10
(十进制)。具体选择哪一个 radix 取决于实现。ECMAScript 5 澄清了应该使用 10 (十进制),但不是所有的浏览器都支持。因此,在使用parseInt
时,一定要指定一个 radix。 - 如果输入的
string
以任何其他值开头,radix
是10
(十进制)。
如果第一个字符不能转换为数字,parseInt
会返回 NaN
。
答案为:
[1, NaN, NaN]
边栏推荐
- MySQL table insert Chinese change? Solution to the problem of No
- 培养中小学生对教育机器人的热爱之心
- Websites that it people often visit
- unable to execute xxx. SH: operation not permitted
- 【毕业季·进击的技术er】年少有梦,何惧彷徨
- Landing guide for "prohibit using select * as query field list"
- IDEA xml中sql没提示,且方言设置没用。
- 面试会问的 Promise.all()
- Pytorch-Yolov5從0運行Bug解决:
- Virtual machine installation deepin system
猜你喜欢
Embedded-c language-9-makefile/ structure / Consortium
社交媒体搜索引擎优化及其重要性
Cannot activate CONDA virtual environment in vscode
Idea automatic package import and automatic package deletion settings
Rhcsa --- work on the third day
unable to execute xxx. SH: operation not permitted
培养中小学生对教育机器人的热爱之心
Deep understanding of lambda expressions
[Yu Yue education] autumn 2021 reference materials of Tongji University
Summary of main account information of zhengdaliu 4
随机推荐
[C language] Dynamic Planning --- from entry to standing up
面试会问的 Promise.all()
Pytoch --- use pytoch to realize u-net semantic segmentation
What methods should service define?
记录一次Unity 2020.3.31f1的bug
Message mechanism -- message processing
洛谷入门3【循环结构】题单题解
Leetcode merge sort linked list
Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
缓存一致性解决方案——改数据时如何保证缓存和数据库中数据的一致性
DMA Porter
Idea automatic package import and automatic package deletion settings
Introduction to Luogu 3 [circular structure] problem list solution
What are the rules and trading hours of agricultural futures contracts? How much is the handling fee deposit?
Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
二叉樹解題(二)
Beginner crawler - biqu Pavilion crawler
奠定少儿编程成为基础学科的原理
Change deepin to Alibaba image source
Pit encountered in win11 pytorch GPU installation