I know I can do this:
a = ['1', '2']
b = []
a.forEach(function(e) { b.push(parseInt(e)); })
b
>> [1, 2]
I want to learn what is wrong in this?
['1', '2'].map(parseInt)
>> [1, NaN]
I know I can do this:
a = ['1', '2']
b = []
a.forEach(function(e) { b.push(parseInt(e)); })
b
>> [1, 2]
I want to learn what is wrong in this?
['1', '2'].map(parseInt)
>> [1, NaN]
You can do that in the following way using map()
by passing Number
:
var res = ['1', '2'].map(Number)
console.log(res);