'How to create a function for adding an object created by the same function into array?

I am creating html game where i generate platforms by manually adding them into the array platforms, therefore I created a function that will adding objects into that array. But it throws an error:

canvas.bundle.js:230 Uncaught TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))

let generation = ( object, array, image, y ) => {
  array = [... array, new object({
    x: LastPos, y, image
  })]
  LastPos = image.width + Math.floor(Math.random() * 1000)
}

generation(Platform,platforms,platformImage,470)
  • The reason to create that function is the attempt to make infinity game (runner)

the function execution will be in the loop

P.S object to be generating is the class:

class Platform {  
  constructor({x, y, image}) {
    this.position = {
      x, y
    }
    this.image = image
    this.width = image.width
    this.heigth = image.height

  }
  draw() {
    c.drawImage(this.image, this.position.x, this.position.y)
  }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source