查看此链接底部的“Tricky Use Case”,其中解释了NaN
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/map
通常使用带有一个参数(被遍历的元素)的回调。一些函数也通常与一个参数一起使用。这些习惯可能会导致令人困惑的行为。
// Consider:
["1", "2", "3"].map(parseInt);
// While one could expect [1, 2, 3]
// The actual result is [1, NaN, NaN]
// parseInt is often used with one argument, but takes two. The second being the radix
// To the callback function, Array.prototype.map passes 3 arguments: the element, the index, the array
// The third argument is ignored by parseInt, but not the second one, hence the possible confusion.
// See the blog post for more details
// Solution:
function returnInt(element){
return parseInt(element,10);
}
["1", "2", "3"].map(returnInt);
// Actual result is an array of numbers (as expected) [1, 2, 3]