Node.js面试题:map(parseInt)
【摘要】 问题
写出输出结果
console.log([1, 2, 3, 4].map(parseInt))
1
输出结果:
1,NaN, NaN, NaN
1
考察知识
1、map
var new_array = arr.map(function callback(currentValue[, index[, array]]) {
// 新数组的返回元素
}[,...
问题
写出输出结果
console.log([1, 2, 3, 4].map(parseInt))
- 1
输出结果:
1,NaN, NaN, NaN
- 1
考察知识
1、map
var new_array = arr.map(function callback(currentValue[, index[, array]]) {
// 新数组的返回元素
}[, thisArg])
参数:
callback:数组迭代的每一项执行的回调函数,可以有三个参数: currentValue:当前迭代项 index:当前迭代项的索引 (可选) array: map 方法调用的数组(可选)
thisObject:在执行回调函数时定义的 this 对象(没有传递或者为 null,将会使用全局对象)。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
2、parseInt
parseInt(string, radix);
解析一个字符串并返回指定基数的十进制整数, 或者NaN
返回NaN的情况:
1. radix 小于 2 或大于 36
2. 第一个非空格字符不能转换为数字。
- 1
- 2
- 3
- 4
- 5
- 6
举一反三
var arr = [10, 18, 0, 10, 42, 23]
arr = arr.map(parseInt)
console.log(arr)
- 1
- 2
- 3
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/119411894
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)