0

Why does invoking Array.prototype.map directly on Array instance o result in an "unmodified" array?

var o = Array(3); // [ undefined, undefined, undefined ]
o.map((x,y) => y*2); //  [ undefined, undefined, undefined ]

Instead, I have to use apply (or call):

Array.apply(0, o).map((x, y) => y*2)); // [ 0, 2, 4 ]

What am I missing?

Finally, an alternative to the above is:

[...o].map((x, y) => y*2); // [ 0, 2, 4]

I presume because this corrects whatever is missing in my original implementation.

3
  • 1
    You have an empty array, so there's nothing to iterate over Commented Apr 15, 2015 at 21:05
  • Most of the Array iterator functions skip uninitialized elements. Commented Apr 15, 2015 at 21:06
  • 1
    if you look at [].map() polyfills (real polyfills, not _ or lodash), you will see something like an if(i in r) filter on the elements that are mapped. Commented Apr 15, 2015 at 21:50

1 Answer 1

4

Why does invoking Array.prototype.map directly on Array instance o result in an "unmodified" array?

Because .map only works on elements that actually exist. Array(3) creates an empty array of length 3. Put differently: .map omites holes.

Instead, I have to use apply (or call): ... What am I missing?

Array.apply(0, o) is equivalent to Array(undefined, undefined, undefined) in your case, i.e. you are creating an array that contains three elements.

The different becomes more apparent if you compare

console.dir(Array(3));
// vs
console.dir(Array.apply(null, Array(3)));

The first one only has property length, the second one also has properties 0, 1 and 2.

Finally, an alternative to the above is: ...

The spread operator will call o[Symbol.iterator]. The iterator of an array will iterate over holes, just like you would when using a normal for loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.