> For the complete documentation index, see [llms.txt](https://demirels-organization.gitbook.io/javascript-tutorial/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://demirels-organization.gitbook.io/javascript-tutorial/arrays-of-objects/sports-game-example.md).

# Sports Game Example

Here is an example of how an array of objects can be used in the context of a sports game:

```javascript
const players = [
  {
    name: 'John',
    position: 'forward',
    goals: 10
  },
  {
    name: 'Jane',
    position: 'midfield',
    goals: 5
  },
  {
    name: 'Bob',
    position: 'defense',
    goals: 2
  }
];

// Calculate the total number of goals scored by the team
let totalGoals = 0;
players.forEach(player => {
  totalGoals += player.goals;
});
console.log(`Total goals scored: ${totalGoals}`); // Output: Total goals scored: 17

// Find the player with the most goals
let topScorer = players[0];
players.forEach(player => {
  if (player.goals > topScorer.goals) {
    topScorer = player;
  }
});
console.log(`Top scorer: ${topScorer.name} (${topScorer.goals} goals)`); // Output: Top scorer: John (10 goals)

// Filter the players by position
const forwards = players.filter(player => player.position === 'forward');
console.log(`Forwards: ${forwards.map(player => player.name).join(', ')}`); // Output: Forwards: John
```

In this example, the `players` array contains objects representing the players on a soccer team. Each object has three properties: `name`, `position`, and `goals`.

The script first calculates the total number of goals scored by the team by iterating over the array of players and adding up the number of goals for each player.

Next, the script finds the player with the most goals by iterating over the array and comparing the number of goals for each player.

Finally, the script filters the players by position by using the `filter()` method and only including players who play the position of 'forward'. The names of the forwards are then extracted using the `map()` method and joined together into a string using the `join()` method.
