FAQ: Objects - Nested Objects

That produces

{ name: 'Bill' }
1 Like

Did you submit that as an answer?

The above is the error I reported in my original post. Since this runs without error in VS Code I know that it is not my code. Nevertheless, I cannot move forward.

1 Like

So this doesn’t pass?

const firstPassenger = spaceship.passengers[0]

Just checked my old work and have a more filled out object:

spaceship.passengers = [
  {name: 'Dave', type: 'human', job: 'pilot'}, 
  {name: 'Frank', type: 'human', job: 'co-pilot'},  
  {name: 'Hal9000', type: 'computer', job: 'systems'}
]

Using your object produced a similar error:

firstPassenger should be assigned to the value at the first index of the spaceship ‘s passenger array.

let spaceship = {
  passengers: 
  [
  {name: 'Dave', type: 'human', job: 'pilot'}, 
  {name: 'Frank', type: 'human', job: 'co-pilot'},  
  {name: 'Hal9000', type: 'computer', job: 'systems'}
]  
  ,
  telescope: {
    yearBuilt: 2018,
    model: "91031-XLT",
    focalLength: 2032 
  },
  crew: {
    captain: { 
      name: 'Sandra', 
      degree: 'Computer Engineering', 
      encourageTeam() { console.log('We got this!') },
     'favorite foods': ['cookies', 'cakes', 'candy', 'spinach'] }
  },
  engine: {
    model: "Nimbus2000"
  },
  nanoelectronics: {
    computer: {
      terabytes: 100,
      monitors: "HD"
    },
    'back-up': {
      battery: "Lithium",
      terabytes: 50
    }
  }
};  
let firstPassenger = spaceship.passengers[0].name // = Dave
let firstPassenger = spaceship.passengers[0] // prints '{ name: 'Dave', type: 'human', job: 'pilot' } '

1 Like

This is the code that passed previously. I don’t want to reset the exercise in case there is a new bug. Would rather leave the old code alone.

let firstPassenger = spaceship.passengers[0]
print(firstPassenger)

If all else fails, compare your code to the solution and adjust to match.

Why, in this exercise does ‘back-up’ appear in yellow and in quotation marks? Why doesn’t it appear the same as the other key-value pairs i.e. turquoise in colour and no quotation marks?

1 Like

Without quotes:

back-up: {
        ^
SyntaxError: Unexpected token -

Dash is an invalid character in property name (an identifier). It’s either,

'back-up'

or

backup

or

back_up

let spaceship = {
passengers: null,
telescope: {
yearBuilt: 2018,
model: “91031-XLT”,
focalLength: 2032
},
crew: {
captain: {
name: ‘Sandra’,
degree: ‘Computer Engineering’,
encourageTeam() { console.log(‘We got this!’) },
‘favorite foods’: [‘cookies’, ‘cakes’, ‘candy’, ‘spinach’] }
},
engine: {
model: “Nimbus2000”
},
nanoelectronics: {
computer: {
terabytes: 100,
monitors: “HD”
},
‘back-up’: {
battery: “Lithium”,
terabytes: 50
}
}
};
const capFave = (spaceship.crew.captain[‘favorite foods’])[0];
spaceship.passenger = [
{name: ‘john’},
{name: ‘nick’}
]
const firstPassenger = spaceship.passenger[0];
console.log(firstPassenger); //prints { name: ‘john’ }
/red line under code “Did you change the passengers property on the spaceship object to be an array?”/

Can not move on.

1 Like

Should that be a plural?

That makes sense. Thanks for clarifying mtf :+1:

1 Like

I created a function that pushes passenger objects to the spaceship.passengers to somehow have a more efficient way of adding them, for practice.
Any suggestions/corrections are welcome!

let addPassengerObject = (theName, theRace) => {
  // create instance of object here with the same keys
  let objectToAdd = {
    name: theName,
    race: theRace
  };

  return objectToAdd;
};

// an array to be assigned to passengers
arrayToPushToObject = []
// pushing values to array
arrayToPushToObject.push(addPassengerObject('Star Lord', 'human'));
arrayToPushToObject.push(addPassengerObject('Thanos', 'alien from Titan'));

// assigning the value of array
spaceship.passengers = arrayToPushToObject;

Thank you!

1 Like

My code for Step 3 evaluated as correct but I didn’t follow the instructions specifying “Make sure to use bracket and dot notation to get the passenger object through nested access (don’t just copy the object into the variable!)” and am having trouble understanding what this means exactly. Here was my code:

let passengerOne = spaceship.passengers[0];

let firstPassenger = passengerOne

My code returns the same value as the solution when I console log it.
Could someone break this down for me?

1 Like

Hi,

I don’t understand the syntax of creating the function

encourageTeam() { console.log(‘We got this!’) }

To create the function, shouldn’t be the syntax something like:

const encourageTeam = () => {console.log(‘We got this!’) }
?

I tried to create function in this way and I got an “unexpected token: {” error.

Thx!

Or since there is syntax for methods introduced on page 6/10 - Methods, maybe the syntax should be

encourageTeam () {console.log(‘We got this!’) }

with the space between encourageTeam and () ?

Does the added space matter or not?

No, it does not matter, to JS. However, it may matter in the lesson checker.

Aside

JS largely ignores white space so don’t be afraid to use it to your advantage in terms of readability and grouping.

  encourageTeam () {
    console.log('We got this!')
  }

Something to keep in mind to help preserve one’s sanity, and that of anybody else whose duty it might be to maintain our code at a later date.

1 Like

This step by step chain of thought really made me think of implementing the same method elsewhere. Really helpful!

1 Like