FAQ: Objects - Looping Through Objects

member is a variable containing a string, so in your example its similar to 'back-up'.

we can only uses keys to look up values in an object, keys itself do not contain information associated information about an object

But back-up is a key that we use to look up it’s value battery. So if member is similar, then member.name should work just as ['back-up'].battery works above.

it does:

for (const member in spaceship.crew) {

the loop simply means we get all the crew names instead of just a single one with a hard-coded key (like back-up)

I’m sorry I know im going in circles but Im really trying to grasp this lol.

Each member in the loop references an object with a name property as I understand

for (const member in spaceship.crew) {

So doing this should work if we just wanted each crew member’s name. Is this logic wrong?

member is not an object. If you attempt to log member and its type:

console.log(member, typeof member);

you will see member is a string, which we can use as key to look up information of spaceship crew

My god I’m stupid, apologies I get it now

Calling a function Looping Through Objects using the For … In

Hey guys, I don’t know if you already passed by this point, by I saw the functions inside of the object spaceship but each one with a different name, so came the question, how to call each function if they don’t follow a pattern like spaceship.crew.translator.name, so if I want to call each one like Shauna has the function powerFuel() I need to call spaceship.crew.translator.powerFuel(). However, as each one has a different function you can loop again Nested loops, ask the type of the object using typeof and call it using spaceship.crew[crewMember][details]()

I hope it will be usefull :wink:

for (let crewMember in spaceship.crew){
  console.log(`${crewMember} - ${spaceship.crew[crewMember].name}: ${spaceship.crew[crewMember].degree}`)

  console.log('---> Getting the type of the attribuites using typeof')
  for (details in spaceship.crew[crewMember]){
    console.log(`${details}: ${typeof(spaceship.crew[crewMember][details])}`)
    if (typeof(spaceship.crew[crewMember][details]) == 'function'){