0

I would like to you Array.prototype.map method on an array of given length to initialise it with the objects. Note that I do not intend to any kind of loop here.

I had an idea to this as

cells = Array(numberOfCells).map(() => new Cell());

However, this does not work. Could somebody point out why and suggest a solution of a similar kind?

Maciej Caputa
  • 1,831
  • 12
  • 20
  • 1
    `Array.from({length: numberOfCells}).map(() => new Cell());` – Tushar Apr 06 '17 at 08:34
  • 2
    https://stackoverflow.com/questions/31737901/why-cant-i-map-new-array3-into-an-array-of-new-values, but use `Array.from(numberOfCells, () => new Cell())` nowadays. – Ry- Apr 06 '17 at 08:34
  • I needed to do `Array.from({length: numberOfCells}).map(() => new Cell());` to make it work. – Maciej Caputa Apr 06 '17 at 08:57

1 Answers1

1

This should work:

cells = Array.from(numberOfCells, () => new Cell());

edit: However, @Ryan was the first to point it out in the comments

Alex Romanov
  • 11,453
  • 6
  • 48
  • 51