1

I am looking to set the

opponent = search_abbreviation

teams is an array of team objects that, before the loop, look like:

var team = {
          teamId: team_id,
          teamAbbreviation: abbreviation,
          gamePlayedId: game_id,
          totalPlaysFor: offensePlays,
          totalYardsFor: offenseYds,
          yardsPerPlayFor: offenseAvgYds,
          opponent: '',
          totalPlaysAgainst: 0,
          totalYardsAgainst: 0,
          yardsPerPlayAgainst: 0
        };

I have the following code to find the opponent by matching the gamePlayedId's to each other, then it will update the defensive(against) stats and the opponent properties. What am I doing wrong that will not allow me to modify the opponent property?

var teams = [
{
          teamId: 0,
          teamAbbreviation: 'abbreviation1',
          gamePlayedId: 11,
          totalPlaysFor: 12,
          totalYardsFor: 13,
          yardsPerPlayFor: 14,
          opponent: 'op',
          totalPlaysAgainst: 0,
          totalYardsAgainst: 0,
          yardsPerPlayAgainst: 0
},
{
          teamId: 1,
          teamAbbreviation: 'abbreviation2',
          gamePlayedId: 11,
          totalPlaysFor: 12,
          totalYardsFor: 13,
          yardsPerPlayFor: 14,
          opponent: 'op2',
          totalPlaysAgainst: 0,
          totalYardsAgainst: 0,
          yardsPerPlayAgainst: 0
}];

const result = teams.forEach(({ teamAbbreviation, gamePlayedId, teamId }) => {
      var search_gamePlayedId = gamePlayedId;
      var search_teamId = teamId;
      var search_abbreviation = teamAbbreviation;
      teams.forEach(({ opponent, gamePlayedId, teamId }) => {
        if (search_gamePlayedId === gamePlayedId && search_teamId !== teamId) {
          opponent = search_abbreviation;
          // Modify more properties
        }
      });
    });
    
console.log(result)

2 Answers 2

2

opponent links to a different memory space than teams[n].opponent in your code.

teams.forEach(({ opponent, gamePlayedId, teamId }, index) => {... // adding index

// then 
teams[index].opponent = 'something'

or

teams.forEach(team => {
    team.opponent = 'something'
})
Sign up to request clarification or add additional context in comments.

Comments

0
teams.forEach( (team1) ) => {
  teams.forEach( (team2) => {
    if (team1.gamePlayedId === team2.gamePlayedId && team1.teamId !== team2.teamId) {
      team2.opponent = team1.teamAbbreviation;
      // Modify more properties
    }
  });
});

1 Comment

I did not think of this but it seems to give me exactly what I am looking for. This is now giving me benefits beyond what I posted. Thanks a ton!

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.