Reputation: 229
I want to create/instantiate an array objectArray
with several objects, whereby the objects shall contain x and y (empty at the beginning).
The length (amount of objects) of objectArray
needs to be the same as the length of i.e. arrayLong
. How I have to implement that?
Finally, it should look like that (etc. corresponding to the length of arrayLong
):
var objectArray = [ { x: 0, y: 0 }, { x: 0, y: 0 }, { x: 0, y: 0 } etc. ];
Upvotes: 1
Views: 103
Reputation: 72967
Simple:
var objectArray = []; // Declare the array variable
for(var i = 0; i < arrayLong; i++){ // Do something `arrayLong` times
objectArray.push({x: 0, y:0}); // Add a object to the array.
}
This assumes arrayLong
is a numeric value, of course.
Another way you could do it, is this:
var objectArray = Array.apply(null, Array(arrayLong))
.map(function(){return {x: 0, y:0}});
Upvotes: 3