Why doesn't this iterate?
(Array(5)).reduce(function(cur,prv,n,a){alert(n)},'');
I never reached the function body, seemingly ignoring all of the empty values, which is not what I want it to do.
As far as I understand Array(5) returns an instance of Array Object with a property length (value 5), but without values. One way to be able to use reduce (or other array methods like map) here is:
String(Array(5)).split(',').reduce(function (cur, prv, n, a) { /* ... */ })
An alternative could be:
Array.apply(null,{length: 5}).map( [callback] );
// or
Array.apply(null,Array(5)).map( [callback] );
But that will encounter a maximum call stack error at some point. The string/split method keeps working (albeit a bit slower) for large arrays. Using node.js on my (not so fast) computer mapping Array(1000000) with the string/split-method lasts 0.47 seconds, the array.apply method for the same crashes with a RangeError (Maximum call stack size exceeded).
You can also write a 'static' Array method to create an Array with n values and all usable/applicable Array methods in place:
Array.create = function (n, mapCB, method, initial) {
method = method in [] ? method : 'map';
var nwArr = ( function(nn){ while (nn--) this.push(undefined); return this; } )
.call([], n);
return mapCB ? nwArr[method](mapCB, initial) : nwArr;
};
// usages
Array.create(5, [callback], 'reduce', '');
Array.create(5).reduce( [callback], '');
Array.create(5).map( [callback] ).reduce( [callback] );
// etc.
As others have stated, the implementation of reduce causes this to throw a TypeError because the calling array has no initial values. To initialize an array with initial values of undefined and successfully invoke the function, try something like this:
Array.apply(undefined, Array(5)).reduce(function(cur,prv,n,a){alert(n)},'');
TypeError: Reduce of empty array with no initial valueundefinedArray.apply(0, Array(5)).reduce