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?

3
  • 1
    Array.from({length: numberOfCells}).map(() => new Cell()); Commented Apr 6, 2017 at 8:34
  • 2
    stackoverflow.com/questions/31737901/…, but use Array.from(numberOfCells, () => new Cell()) nowadays. Commented Apr 6, 2017 at 8:34
  • I needed to do Array.from({length: numberOfCells}).map(() => new Cell()); to make it work. Commented Apr 6, 2017 at 8:57

1 Answer 1

1

This should work:

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

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.