i have a question about how a number was also associated with a string literal.
My program features a list of scorers in my game object as you can see here.
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,
},
};
let count0 = 0;
let count1 = 0;
let count2 = 0;
for (let i = 0; i < playerScored.length; i++) {
if ( playerScored[i] === 'Lewandowski' ) {
count0 += 1;
}
else if ( playerScored[i] === 'Gnarby') {
count1 += 1;
}
else if ( playerScored[i] === 'Hummels') {
count2 += 1;
}
}
const scorers = {};
for (const player of game.scored) {
scorers[player] ? scorers[player]++ : (scorers[player] = 1);
}
console.log(scorers);
My question is, in this particular line of code in the program
const scorers = {};
for (const player of game.scored) {
scorers[player] ? scorers[player]++ : (scorers[player] = 1);
}
console.log(scorers);
How did the program show the number as well as the name of the player who scored? I kind of understand how the player name is put into the empty object. Thank you.
?:operator does?playerScoreddefined?