Trying to adopt a more functional programming style, I often run into the challenge of populating an array with n
items.
Here is an example:
const items[];
for (let i = 0; i < n; i++) {
items.push(new Item());
}
As far as I know, there are two side effects here: i
is being mutated and so is items
.
I am curious about the "pure" way to achieve this in JavaScript. I have experimented with methods like (new Array(n)).map(...)
and (new Array(n)).forEach(...)
, but I am unsure of their advantages or disadvantages. Can anyone explain further, or direct me to a resource that delves into this topic?