map Passes Three Arguments To The Callback
map invokes the callback with (value, index, array), so a bare parseInt reference silently parses in radix 1 and 2; wrap it in an arrow or use Number.
Code
// map 的回调实际收到三个参数 (value, index, array)
// parseInt(string, radix) 恰好把 index 当 radix:radix 1 不存在 → NaN
const raw = ['10', '10', '10'];
console.log('accidental:', raw.map(parseInt));
// 修复:显式包一层,只把字符串传进去
console.log('fixed arrow:', raw.map((s) => parseInt(s, 10)));
console.log('fixed Number:', raw.map(Number));
// 想看全貌:显式声明三个参数
const logged = raw.map((value, index, array) => value + '@' + index + '/' + array.length);
console.log('full signature:', logged);Output
Captured from a sandboxed run of this snippet in the build pipeline.
accidental: [ 10, NaN, 2 ]
fixed arrow: [ 10, 10, 10 ]
fixed Number: [ 10, 10, 10 ]
full signature: [ '10@0/3', '10@1/3', '10@2/3' ]How the snippet works
Top-level bindings raw, logged hold the demo state — nothing sits inside a function scope.
Using it
The demo output is labeled (accidental:, fixed arrow:, fixed Number:), so every printed line maps back to its originating operation.
Key statements
const logged = raw.map((value, index, array) => value + '@' + index + '/' + array.length);
Interactive Tools for This Topic
FAQ
- Will the snippet run without installing packages?
- Yes, it runs as-is — nothing outside the built-in APIs is referenced. Its state lives in module-level bindings: `raw`, `logged`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.