(['1','2','3']).map(n => parseInt(n));
will return expect result [1,2,3]
But:
(['1','2','3']).map(parseInt);
returns [1, NaN, NaN]
.
Where is wrong?
(['1','2','3']).map(n => parseInt(n));
will return expect result [1,2,3]
But:
(['1','2','3']).map(parseInt);
returns [1, NaN, NaN]
.
Where is wrong?
As Array#map
callback has 3 arguments, second argument is index
which is causing this result. No matter what function
you pass as callback
, this arguments are passed as parameters for that function.
Second argument for parseInt
is radix
hence parseInt('2',1)
and parseInt('3',2)
is NaN
Execution flow will be:
console.log((['1', '2', '3']).map(function(currenValue, index, array) {
return parseInt(currenValue, index, array);
}));
I will suggest you to go with the Number
console.log((['1', '2', '3']).map(Number));