JavaScript map+parseInt 容易产生的误区

时间:2020-07-23 01:46:16   收藏:0   阅读:71

map

/**
 * 语法:
 * var new_array = arr.map(function callback(currentValue[,index[,array]]){
 *  // return element for new_array
 * }[,thisArg])
 */

简单示例

var numbers = [1, 4, 9]
var double = numbers.map(x => x * 2)
console.info(double)

parseInt in map

const newIntArray = ["1", "2", "3"].map(parseInt)
console.info(newIntArray) // [ 1, NaN, NaN ]

解析过程

解决办法: 使用箭头函数

const newIntArrayWithNumber = ["1.2", "2.3", "3.4"].map(num => parseInt(num, 10))
console.info(newIntArrayWithNumber) // [ 1, 2, 3 ]

原文:https://www.cnblogs.com/leslie1943/p/13363962.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!