I'm want to quickly construct an array of n
length using the array constructor Array()
and then loop over the resulting array.
Per MDN's 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. If the argument is any other number, a RangeError exception is thrown.
Presumably, executing Array(5)
creates an array with a length of 5, which it does.
var arr = new Array(5);
console.log(arr); // [undefined x 5]
console.log(arr.length); // 5
However, when I try to loop over the resulting array and log out the values or the index, nothing happens.
arr.forEach(function(v, i) { console.log(v, i); });
// nothing logs to the console
Alternatively, if I use an array literal, and try to loop over the values, it logs as expected:
[undefined, undefined].forEach(function(v, i) { console.log(v, i); });
// undefined 0
// undefined 1
Why can't I loop over an array created by the Array constructor?
This answer explains some of the browser strangeness that occurs with map
, example:
arr.map(function(v, i) { return i; }) // returns [undefined x 5]
But I'm particularly interested in why the forEach
loop doesn't iterate at all over the values.
I understand I am not answering to the question directly, bit still I believe this provides good information.
Check what Kyle Simpson said recently about this topic.
Basically,
console.log(new Array(5)) // Chrome: [undefinedx5] Firefox: [ <5 empty slots> ]
and
console.log([undefined, undefined, undefined, undefined, undefined]) // [ undefined, undefined, undefined, undefined, undefined ]
are entirely different value types. And the browser is (kind of) lying to you when it says undefinedx5. Applying .map() over the first case will return nothing, while in the second case you can do operations with the undefined.
From the spec, ecma-262/5.1/#sec-15.4.2.2, the only thing new Array(5)
does, is to create an object of class type Array with length property = 5. The literal array syntax, [], actually places the types (if you give it something) in the slots.
That's because ES5 array methods skip missing indexes. That is, they iterate i
from 0
to length-1
, but at each iteration they check if the array has an i
own property.
If you want it to work, you can use fill
to create the indices.
var arr = new Array(5).fill();
arr.forEach(function(v, i) { console.log('arr['+i+'] = ' + v); });
Looks like you have a typo in your code:
arr.forEach(functio(v, i) {
Use this instead:
arr.forEach(function(v, i) {
UPDATE
forEach() executes the provided callback once for each element present in the array in ascending order. It is not invoked for index properties that have been deleted or are uninitialized (i.e. on sparse arrays). MDN article
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With