Return property from object

Map to sub-property created by separate method – Codecademy Project Team Stats

Resources

code

Issue

undefined method line 31

get teamPoints() {
  return this._games.teamPoints;
  },

console.log(team.teamPoints) //undefined

Goal

I would like to create an array exclusively of teamPoints.

Walk-Through

object team is created to contain _players & _games arrays. Our focus for this post is the _games array.

Method addGames() line 22 creates the teamPoints sub-property.

addGames(opponent, teamPoints, opponenetPoints) {
    const gameInfo = {
      opponent: opponent,
      teamPoints: teamPoints,
      opponentPoints: opponenetPoints,
    }
    return this._games.push(gameInfo);
  },

How is team._games.teamPoints accessed?

Outcome Expected

{{teamPoints: 54}, {teamPoints: 100}, {teamPoints:1}
//maybe different that expected

Thank you in advance!

1 Like

Is this from a codecademy exercise or is it a separate project?

1 Like

Codecademy Project Team Stats

2 Likes

An individual game maybe accessed by array index

get teamPoints() {
   	return this._games[0];
 }, // { opponent: 'Lakers', teamPoints: 54, opponentPoints: 36 }

however

get teamPoints() {
   	return this._games[0,0];
 }, //undefined
1 Like

Here’s how you would do it:

console.log(team._games[0].teamPoints)

Outputs:

54

You can’t get items by [0,0]. If you want to access an item from an array within an array, you need to use the item name.

Of course, this isn’t the solution because you can only access one item at a time, but that’s how you do one individual item at a time.

2 Likes

for loop:

loops through a block of code a number of times.

  teamPoints() {
    for (let i = 0; i < this._games.length; i++) {
      return this._games[i].teamPoints;
    }
  } 
console.log(team.teamPoints()); //54

for/in` loop:

The JavaScript for/in statement loops through the properties of an object:

  teamPoints() {
    for (let x in this._games) {
      return this._games[x].teamPoints;
    }
  } 
console.log(team.teamPoints()); //54

54 is the teamPoints of the first object item.
team.addGames('Lakers', 54, 36);
Thoughts on why the loop does not appear to be looping?

2 Likes

This is because return always stops a loop or if else statement. Try using console.log instead.

2 Likes

_games is a property of an object, but the value of _games is an array containing objects. To iterate through your _games array you should either use the for loop as you described or possibly a .forEach() method. I found the following link especially helpful: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Indexed_collections#Iterating_over_arrays

2 Likes

But doesn’t the array have an array with the main array? So running the with just _games gets you all the games information.

1 Like

The _games array contains these gameInfo objects: (objects within an array as opposed to array within an array).
_games = [{opponent: value, teamPoints: value, opponentPoints: value}, {opponent: value, teamPoints: value, opponentPoints: value}, {opponent: value, teamPoints: value, opponentPoints: value}]
From the outcome expected:

it appears there are 3 gameInfo objects in the array.
@letsgetitdone’s for loop:

  teamPoints() {
    for (let i = 0; i < this._games.length; i++) {
      return this._games[i].teamPoints;
    }
  }

will produce the desired outcome with a little tweaking: (Not exactly sure how he wants it. This would return an array containing only teamPoints.)

  teamPoints() {
    let teamPointsArray = [];
    for (let i = 0; i < this._games.length; i++) {
       teamPointsArray.push(this._games[i].teamPoints);
    }
    return teamPointsArray;
  }
  console.log(teamPoints());
  // Output: [54, 100, 1] or whatever the values are.
3 Likes