I have made an object prototype for which I want to combine a set of arrays to create under said object.
I've tried making the object prototype with a function contructor with the needed properties as parameters. Then I created arrays with the data for all the properties. My problem is that when I create a loop my first idea was to create a variable and name it after the item relevant for each object instance (In this case the name of the football player), but dynamic variable naming is not possible, so I cannot seem to create multiple variables / object instances in a loop.
var Players = function(name, position, number, yearOfBirth) {
this.name = name;
this.position = position;
this.number = number;
this.yearOfBirth = yearOfBirth;
}
var playerNames = ['1','2',....'25'];
var playerPosition = [....];
var playerNumber = [....];
var playerYearOfBirth = [....];
for (var i = 0; i < playerNames.length; i++) {
var playerNames[i] = new Object(playerNames[i], playerPosition[i], playerNumber[i],
playerYearOfBirth[i]);
}
So this of course doesn't work, so I also tried this.
var player = [];
for (var i = 0; i < playerNames.length; i++) {
var players = {}
player.name = playerNames[i];
player.position = playerPosition[i];
player.number = playerNumber[i];
player.yearOfBirth = playerYearOfBirth[i]
player.push(players[i]);
}
but this only applies data to the last object.
So I expected my output to be that it filled in all the objects with the right data, but I wonder if I'm trying to create an unneccesary solution. I haven't played around with objects before, so I'm just trying to get around it and see how it is applied outside of tutorials, when I actually have a usecase for it.
var, replaceObjectwithPlayer, then it sould workplayerandplayers