Riddle me this.
Let's say you new up an array of 1,000 elements and want to assign the index of each element as the element's value using .map()
. I would expect it to work like so:
new Array(1000).map((element, i) => i);
Here's the problem: the new array returned, while having the required 1,000 elements, is empty, despite assigning the .map()
's return value to be the array index. This can be remedied by chaining .fill(0)
like so:
new Array(1000).fill(0).map((element, i) => i);
But why?
Why do .map
or .filter
require a populated array to produce a populated array?