2

Anyone can explain me difference between new Array(7) and Array.apply(null, Array(7))? In context:

Array.apply(null, Array(7)).map((e, i) => {
  return moment(i, 'e').format('ddd');
}); // ["Sun" ,"Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]

new Array(7).map((e, i) => {
  return moment(i, 'e').format('dd');
}); // [empty × 7]
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment-with-locales.min.js"></script>

5
  • I'm getting an error when that codes runs. "TypeError: (new Array(...)).apply is not a function", Commented Dec 28, 2017 at 22:18
  • @user184994, edited Commented Dec 28, 2017 at 22:20
  • 1
    Possible duplicate of JavaScript "new Array(n)" and "Array.prototype.map" weirdness Commented Dec 28, 2017 at 22:22
  • check the link in the comment above. The top answer goes into exactly why this happens Commented Dec 28, 2017 at 22:25
  • no it's not I wanna know why Array.apply() work and new Array() doesn't work, why we should pass parameters(null and Array7)? Commented Dec 28, 2017 at 22:28

3 Answers 3

3
 Array(7)

creates a Sparse array with no elements set. So as an object it would look like:

 {length: 7}

If you would call any of the new cool array methods, it wouldnt iterate as there are no elements in the array. However if that is spread into the Array constructor:

  Array(...Array(3))

Its equal to

 Array(undefined, undefined, undefined)

And that actually creates 3 (or 7) undefined array slots:

 {
 0:undefined,
 1:undefined,
 2:undefined,
 length:3
 }

And now you can iterate over it with map etc. The same is possible with:

 Array(7).fill().map(func)

and

 Array.from({length:7}, func);
Sign up to request clarification or add additional context in comments.

Comments

3

Array.apply calls the prototype method on the Array object. Where as doing Array(7) is creating a new array of seven elements, as such new Array(7).apply is not a valid call and would give the following error:

Uncaught TypeError: (intermediate value).apply is not a function

Doing Array.apply(null, Array(7)) is the only valid call here.

Edit

The statement Array.apply(null, Array(7)) will create an array populated with undefined elements. As such calling .map() will iterate those elements and produce your expected result.

On the other hand calling just new Array(7) will create a sparse array with a define length of 7. Meaning the length will be defined as 7 but it didn't populate any elements into the array. So there is nothing to iterate over yet.

2 Comments

prototype method it is like static method?
Array is a function!
1

these two approach are equivalent since Function.prototype.apply is part of the Function api.

it looks you likely need something like:

Array
  .from({ length: 7 }, (_, i) => moment(i, 'e').format('ddd'))
;

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.