const game = {
team1: 'Real Madrid',
team2: 'Barcelona',
players: [
[
'Courtois',
'Carvajal',
'Ramos',
'Varane',
'Marcelo',
'Casemiro',
'Kroos',
'Modric',
'Vinicius',
'Benzema',
'Rodrygo',
],
[
'Ter Stegen',
'Sergi Roberto',
'Pique',
'Lenglet',
'Jordi Alba',
'Busquets',
'De Jong',
'Pedri',
'Messi',
'Dembele',
'Griezmann',
],
],
score: '3:1',
scored: ['Benzema', 'Benzema', 'Vinicius', 'Messi'],
date: 'Dec 25th, 2037',
odds: {
team1: 1.55,
x: 3.0,
team2: 5.0,
},
};
1) Iterate through the game.scored array and display each player's name on the screen along with the goal number starting from 1 (Goal 1: etc)
This is my preferred solution which gives me the desired output:
for (const [i, v] of game.scored.entries())
{
console.log(`Goal: ${i + 1} ${v}`);
}
Output:
Goal: 1 Benzema
Goal: 2 Benzema
Goal: 3 Vinicius
Goal: 4 Messi
Result Display:
However, I encountered an issue when attempting a different approach using Object.entries() as it did not start the iteration from 1
Solution 2
for (const [i, v] of Object.entries(game.scored))
{
console.log(`Goal: ${i+1} ${v}`);
}
Output:
Goal: 01 Benzema
Goal: 11 Benzema
Goal: 21 Vinicius
Goal: 31 Messi
Result Display: