DEV Community

Discussion on: Here's Why Mapping a Constructed Array in JavaScript Doesn't Work

Collapse
 
loilo profile image
Florian Reuschel • Edited

To be honest, with regard to the article, I find it quite weird that Array(5).keys() does work. 🤔

Thread Thread
 
jbristow profile image
Jon Bristow

It doesn’t!

The ... fires first and produces Array.apply(null, Array(5)).keys()

Then that is expanded to Array(undefined, undefined, undefined, undefined, undefined).keys().

Since the Array was created by a list of items, keys returns [0,1,2,3,4]

JavaScript needs a range operator or a Texas range notation. Just so I don’t need to hide the splat under a function to increase readability.

Thread Thread
 
loilo profile image
Florian Reuschel

Makes sense, thanks. I struggle with splat operator execution order from time to time.

Thread Thread
 
jbristow profile image
Jon Bristow

In trying to rewrite the above code I ended up having to experiment a bit to figure out what was happening when... it’s certainly far from obvious.