0
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: enter image description here

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: enter image description here

2
  • You can use ${parseInt(i)+1} Commented Dec 31, 2020 at 12:28
  • Finally, I got my answer: In the array, we use the entries method but in the object, we have to use the object.entries() method and then pass into the function, the object that we are interested in. Commented Dec 31, 2020 at 12:44

2 Answers 2

1

The first approach takes the array and its entries (Array#entries), whereas the second one takes the object and the entries from the object (Object.entries).

Objects has only strings or symbols as keys. To take a number as value you need to convert it to a number. The shortest way is to take an unary plus +.

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 } };


for (const [i, v] of Object.entries(game.scored)) {
    console.log(`Goal: ${+i + 1} ${v}`);
}

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

Comments

0

game.scored.entries() returns an iterator object in which the indexes are returned as numbers. So adding 1 to the value of i performs the addition and gives you the expected result.

Object.entries(game.scored) returns the key as a string. So when you do i + 1 in the second code example, instead of doing an addition, your code performs concatenation. Since i is a string, 1 will be converted to string and then concatenated with the value of i.

To get the desired result, convert i to a number before adding 1 to it.

There are multiple ways to convert a string to a number. You can use one of the following ways:

  • Using + unary operator

    for (const [i, v] of Object.entries(game.scored)) 
    {
      console.log(`Goal: ${(+i)+1} ${v}`);
    }
    
  • Using parseInt() function

    for (const [i, v] of Object.entries(game.scored)) 
    {
      console.log(`Goal: ${parseInt(i) + 1} ${v}`);
    }
    
  • Using Number() constructor

    for (const [i, v] of Object.entries(game.scored)) 
    {
      console.log(`Goal: ${Number(i) + 1} ${v}`);
    }
    

Comments

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.