Here's a basic function in JavaScript which should convert an array of numeral strings into an array of integers.
function convert(arr) {
return arr.map(parseInt,10);
}
The result of running
convert(["1","2","3","4","5"]);
should be to return
[1,2,3,4,5]
. However the function returns:
[1, NaN, NaN, NaN, NaN]
I know that if the parseInt() function encounters a character which is not a numeral, it returns NaN. I also know that the method will work if the Array.prototype.map() function is replaced with a conventional for () loop, so the culprit seems to be the .map() function. Does anyone know why .map() works normally on the first indice in the array but does not work on the others?
Thanks.