0

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.

3
  • 1
    Remove the var, replace Object with Player, then it sould work Commented Sep 16, 2019 at 10:49
  • 1
    Looks like you confused players and player? Commented Sep 16, 2019 at 10:49
  • "but this only applies data to the last object." you mixed up player and players Commented Sep 16, 2019 at 10:49

4 Answers 4

1

Try this. The array and the object were used in place of each other thus giving error

var players = [];
playerNames=[1,2]
playerPosition=[1,2]
playerNumber=[1,2]
playerYearOfBirth=[1,2]

for (var i = 0; i < playerNames.length; i++) {
  var player = {}
  player.name = playerNames[i];
  player.position = playerPosition[i];
  player.number = playerNumber[i];
  player.yearOfBirth = playerYearOfBirth[i]
  players.push(player);
}
console.log(players)

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

Comments

1
var players = []; // the list of the players
for (var i = 0; i < playerNames.length; i++) {
  var player = {} // the object player
  player.name = playerNames[i];
  player.position = playerPosition[i];
  player.number = playerNumber[i];
  player.yearOfBirth = playerYearOfBirth[i]
  players.push(player); // add the object player into the list of players
}

Comments

1

Your constructor only represents the information about one player, so rename that to Player. You can then set up a new players array to which you can push new players as they are created.

function Player(name, position, number, yearOfBirth) {
  this.name = name;
  this.position = position;
  this.number = number;
  this.yearOfBirth = yearOfBirth;
}

const players = [];

const playerNames = ['Bob', 'Sue'];
const playerPosition = [1, 12];
const playerNumber = ['Forward', 'Goal'];
const playerYearOfBirth = [1972, 1956];

for (var i = 0; i < playerNames.length; i++) {
  const player = new Player(playerNames[i], playerPosition[i], playerNumber[i], playerYearOfBirth[i]);
  players.push(player);
}

console.log(players);

Comments

1

You could collect all players in an object and access the player by the name.

function Player(name, position, number, yearOfBirth) {
    this.name = name;
    this.position = position;
    this.number = number;
    this.yearOfBirth = yearOfBirth;
}

var playerNames = ['foo', 'bar', 'baz'],
    playerPosition = [3, 2, 1],
    playerNumber = [10, 11, 12],
    playerYearOfBirth = [2000, 1998, 1999],
    players = {};

for (var i = 0; i < playerNames.length; i++) {
    players[playerNames[i]] = new Player(
        playerNames[i],
        playerPosition[i],
        playerNumber[i],
        playerYearOfBirth[i]
    );
}

console.log(players);
.as-console-wrapper { max-height: 100% !important; top: 0; }

1 Comment

I'll try this out as well. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.