front-end-interview-questions icon indicating copy to clipboard operation
front-end-interview-questions copied to clipboard

【JavaScript】['1', '2', '3'].map(parseInt)

Open yayxs opened this issue 3 years ago • 1 comments

yayxs avatar Dec 24 '20 10:12 yayxs

parseInt。parseInt()函数更专注于字符串是否包含数值模式。字符串最前面的空格会被 忽略,从第一个非空格字符开始转换。 不同的数值格式很容易混淆,因此 parseInt()也接收第二个参数,用于指定底数(进制数)。如 果知道要解析的值是十六进制,那么可以传入 16 作为第二个参数,以便正确解析 let res = ['1', '2', '3'].map(parseInt)

let arr = ['1','2','3'] let i =0; for(;i<arr.length;i++){ // console.log(parseInt(arr[i], i)) }

console.log(parseInt('1',0)) console.log(parseInt('2',1)) console.log(parseInt('3',2))

yayxs avatar Dec 24 '20 10:12 yayxs