const game = {
team1: 'Bayern Munich',
team2: 'Borrussia Dortmund',
players: [
[
'Neuer',
'Pavard',
'Martinez',
'Alaba',
'Davies',
'Kimmich',
'Goretzka',
'Coman',
'Muller',
'Gnarby',
'Lewandowski',
],
[
'Burki',
'Schulz',
'Hummels',
'Akanji',
'Hakimi',
'Weigl',
'Witsel',
'Hazard',
'Brandt',
'Sancho',
'Gotze',
],
],
score: '4:0',
scored: ['Lewandowski', 'Gnarby', 'Lewandowski', 'Hummels'],
date: 'Nov 9th, 2037',
odds: {
team1: 1.33,
x: 3.25,
team2: 6.5,
},
};
1) Loop over the game.scored array and print each player name to the console,along with the goal number start from 1 like (Goal 1: etc)
This is my first solution and the result is perfect:
for (const [i, v] of game.scored.entries())
{
console.log(`Goal: ${i + 1} ${v}`);
}
Output:
Goal: 1 Lewandowski
Goal: 2 Gnarby
Goal: 3 Lewandowski
Goal: 4 Hummels
Picture Result:
The issue is when I tried from different method using Object.entries() the itration don't start from 1
Soultion 2
for (const [i, v] of Object.entries(game.scored))
{
console.log(`Goal: ${i+1} ${v}`);
}
Output:
Goal: 01 Lewandowski
Goal: 11 Gnarby
Goal: 21 Lewandowski
Goal: 31 Hummels
Picture Result: