当前位置:网站首页>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]
边栏推荐
- Ten thousand volumes are known to all, and one page of a book is always relevant. TVP reading club will take you through the reading puzzle!
- Getting started with pytest -- description of fixture parameters
- Realize the function of data uploading
- Leetcode merge sort linked list
- Use of Baidu map
- DMA Porter
- Promise all()
- Getting started with pytest ----- confitest Application of PY
- C - derived classes and constructors
- IDEA xml中sql没提示,且方言设置没用。
猜你喜欢
Websites that it people often visit
What data does the main account of Zhengda Meiou 4 pay attention to?
万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!
农业生态领域智能机器人的应用
Free drawing software recommended - draw io
Rhcsa --- work on the third day
Summary of common string processing functions in C language
Mapping location after kotlin confusion
The core idea of performance optimization, dry goods sharing
Leetcode- insert and sort the linked list
随机推荐
C - derived classes and constructors
Detailed process of DC-1 range construction and penetration practice (DC range Series)
Oracle stored procedure and job task setting
社交媒体搜索引擎优化及其重要性
Let正版短信测压开源源码
正大留4的主账户信息汇总
Social media search engine optimization and its importance
TypeScript类的使用
Leetcode- insert and sort the linked list
培养中小学生对教育机器人的热爱之心
解析少儿编程中的动手搭建教程
06 装饰(Decorator)模式
Pytorch-Yolov5從0運行Bug解决:
Deep understanding of lambda expressions
Ognl和EL表达式以及内存马的安全研究
二叉树解题(二)
geotrust ov多域名ssl证书一年两千一百元包含几个域名?
Future trend of automated testing ----- self healing technology
DJB Hash
Common errors of dmrman offline backup