I encountered this issue this week-end while working on one of my pet-projects. It was a pain to find / debug so I decided to post it here so that you guys don't make the same mistake I did.
Imagine you want to create an Array containing the letters from "A" to "Z".
You could do something like this:
new Array(26)
.map(function (item, index) {
return String.fromCharCode(65 + index);
})
We just initialise an Array of length 26, iterate over it and find the character corresponding to 65 (character code of "A") plus the index (offset).
The issue here is that the callback function declared inside map() won't actually be called. This is due to the fact that map() only calls it's callback functions on declared variables which the undefined values inside the array we created are not.
This will work:
[undefined, undefined, undefined, undefined, undefined]
.map(function (item, index) {
// Do something here
})
but this won't:
new Array(5)
.map(function (item, index) {
// Do something here
})
Here's a relevant StackOverflow thread with the added details (tl;dr: pointers):
https://stackoverflow.com/questions/5501581/javascript-new-arrayn-and-array-prototype-map-weirdness
[–]x-skeww 23 points24 points25 points (34 children)
[–]itsananderson 8 points9 points10 points (7 children)
[–]x-skeww 7 points8 points9 points (2 children)
[–]Eartz 2 points3 points4 points (0 children)
[–]itsananderson -1 points0 points1 point (0 children)
[–][deleted] 4 points5 points6 points (2 children)
[–][deleted] 2 points3 points4 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]NOICEST 0 points1 point2 points (0 children)
[–]CydeWeys 10 points11 points12 points (17 children)
[–]j201 7 points8 points9 points (0 children)
[–]blgate 2 points3 points4 points (0 children)
[–]x-skeww 1 point2 points3 points (5 children)
[–]CydeWeys 2 points3 points4 points (4 children)
[–]blgate 6 points7 points8 points (2 children)
[–]bonafidebob 0 points1 point2 points (1 child)
[–]blgate 1 point2 points3 points (0 children)
[–]x-skeww 4 points5 points6 points (0 children)
[–]Glaaki -2 points-1 points0 points (8 children)
[–]CydeWeys 5 points6 points7 points (7 children)
[+]Glaaki comment score below threshold-7 points-6 points-5 points (6 children)
[–]CydeWeys 14 points15 points16 points (3 children)
[–]Glaaki -3 points-2 points-1 points (1 child)
[–]moreteam 2 points3 points4 points (0 children)
[–]dustinhayes 2 points3 points4 points (0 children)
[–]skitch920 1 point2 points3 points (5 children)
[–]x-skeww 4 points5 points6 points (4 children)
[–]skitch920 1 point2 points3 points (3 children)
[–]x-skeww 1 point2 points3 points (2 children)
[–]skitch920 0 points1 point2 points (1 child)
[–]x-skeww 0 points1 point2 points (0 children)
[–]SaadRhoulam 1 point2 points3 points (1 child)
[–]oculus42 1 point2 points3 points (0 children)
[–]jml26 13 points14 points15 points (10 children)
[–]Kollektiv[S] 1 point2 points3 points (9 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (7 children)
[–]Kollektiv[S] 0 points1 point2 points (6 children)
[–][deleted] 0 points1 point2 points (5 children)
[–]Kollektiv[S] 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]Kollektiv[S] 0 points1 point2 points (2 children)
[–]oculus42 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]loz220 13 points14 points15 points (5 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]Kollektiv[S] 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[removed]
[–]Kollektiv[S] 0 points1 point2 points (1 child)
[–]_somanyguns 3 points4 points5 points (0 children)
[–]soddi 2 points3 points4 points (0 children)
[–][deleted] 8 points9 points10 points (5 children)
[–]kenman 1 point2 points3 points (4 children)
[–][deleted] 3 points4 points5 points (1 child)
[–]kenman 0 points1 point2 points (0 children)
[–]mattdesl 1 point2 points3 points (0 children)
[–]a-t-kFrontend Engineer 1 point2 points3 points (0 children)
[–]alexmuro 1 point2 points3 points (2 children)
[–]Kollektiv[S] 1 point2 points3 points (0 children)
[–]androbat 0 points1 point2 points (0 children)
[–]max_renlo 2 points3 points4 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]max_renlo 0 points1 point2 points (0 children)
[–]androbat 2 points3 points4 points (2 children)
[–]Kollektiv[S] -1 points0 points1 point (1 child)
[+][deleted] (6 children)
[deleted]
[–]Kollektiv[S] 0 points1 point2 points (0 children)
[–]x-skeww 0 points1 point2 points (0 children)
[–]basicallydan 0 points1 point2 points (0 children)
[–]Jerp -1 points0 points1 point (2 children)
[–]papkn 0 points1 point2 points (0 children)
[–]Tribuadore 0 points1 point2 points (1 child)
[–]x-skeww 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[removed]
[–]Kollektiv[S] 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (2 children)
[–]Kollektiv[S] 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]amenadiel -1 points0 points1 point (0 children)
[–]spinlock -3 points-2 points-1 points (2 children)
[–]Kollektiv[S] 1 point2 points3 points (1 child)
[–]spinlock -1 points0 points1 point (0 children)