I'm trying to write code in a more functional style, and a problem I keep encountering is populating an array of n
items.
For example:
const items[];
for (let i = 0; i < n; i++) {
items.push(new Item());
}
If I understand correctly, there are two side effects here: i
is mutating and items
is as well.
I'm wondering what is the "pure" way to do this in Javascript. I've tried stuff like (new Array(n)).map(...)
and (new Array(n)).forEach(...)
but I'm not sure why these would be better or worse. Can someone elaborate, or point me to a post that covers this topic?