Home > other >  Why does the result of accessing this object property's value give separate letters? and how to
Why does the result of accessing this object property's value give separate letters? and how to

Time:01-07

const game = {
  team1: "Bayern Munich",
  team2: "Borrussia Dortmund",
  odds: {
    team1: 1.33,
    team2: 6.5,
  },
};

for (const [team, odd] of Object.entries(game.odds)) {
  console.log(`Odd of victory ${Object.values(game[team])}: ${odd}`);
}

The result in the console is as follows.....

Odd of victory B,a,y,e,r,n, ,M,u,n,i,c,h: 1.33
Odd of victory B,o,r,r,u,s,s,i,a, ,D,o,r,t,m,u,n,d: 6.5

what is the wrong with that code and how to fix this problem?

CodePudding user response:

just remove Object.values from inside the loop

for (const [team, odd] of Object.entries(game.odds)) {
    console.log(`Odd of victory ${game[team]}: ${odd}`);
}
  • Related