I want to create empty array with fixed length, and then use .map
on it to return new array. However, it's not working as expected.
According to mdn docs:
If the only argument passed to the Array constructor is an integer between 0 and 232-1 (inclusive), this returns a new JavaScript array with length set to that number.
new Array(3)
returns [undefined × 3]
. Shouldn't it be: [undefined, undefined, undefined]
?
Let's consider following examples:
1) Not working.
var a = new Array(3);
a.map((x, i) => i); // [undefined × 3]
2) Working.
var a = Array.apply(null, new Array(3));
a.map((x, i) => i); // [0, 1, 2]
I tested this on latest Google Chrome Canary.