Home > Net >  Filling empty javascript objects?
Filling empty javascript objects?

Time:06-10

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.

CodePudding user response:

The line

  scorers[player] ? scorers[player]   : (scorers[player] = 1);

is equivalent to

if (scorers[player]) {
    scorers[player]  ;
} else {
    scorers[player] = 1;
}

The if condition checks whether the name is already in the object. If it is, it uses to add 1 to the count. If the name isn't already in the object, it's created with the assignment in the else block, and given an initial count of 1.

CodePudding user response:

The scorers object is something like this:

scorers = {
    pique: 1,
    messi: 3,
    henry: 11
};

when you do scorers[player] for example scorers['pique'] It adds one to the pique values. and your object will become

scorers = {
    pique: 2,
    messi: 3,
    henry: 11
};

and when you do scorers[player] = 1 in your code, in fact you're adding a new property to the scorers object. for example after scorers['xavi'] = 1 your object becomes:

scorers = {
    pique: 2,
    messi: 3,
    henry: 11,
    xavi: 1
};

CodePudding user response:

Your game.scored looks like this:

const game = {
  scored: ['Lewandowski', 'Gnarby', 'Lewandowski', 'Hummels']
}

When you run the following code:

const scorers = {};
for (const player of game.scored) {
  scorers[player] ? scorers[player]   : (scorers[player] = 1);
}
console.log(scorers);

It will loop through ['Lewandowski', 'Gnarby', 'Lewandowski', 'Hummels'] and checks if there is scorers[player]. If it exists, it just increments it by 1, otherwise, sets it to 1.

Hence, after your loop is over and you console.log it, scorers will have 'Lewandowski', 'Gnarby', 'Hummels' as its keys.

I'm confused with how it has the name as well as the number in the same line, I assume adding the appends the string value with the number value as well

You are basically creating an object that looks like this:

scorers = {
 Lewandowski: 2,
 Gnarby: 1,
 Hummels: 1
}

Maybe it will be easier to understand if you print the value of scorers every loop.

const game = {
  scored: ['Lewandowski', 'Gnarby', 'Lewandowski', 'Hummels']
}

const scorers = {};
for (const player of game.scored) {
  scorers[player] ? scorers[player]   : (scorers[player] = 1);
  console.log(scorers);  
}

  • Related