Why can't I use crewMember.name?

close, the variable contains a string value, and when dealing with strings you have to use the associative array notation:

variable = 'key'
object[variable]
// or directly with a string:
object['key']
4 Likes

but why do you have to put spaceship.crew again I thought varName was already defined as being spaceship.crew

for(let varName in spaceship.crew){
console.log(${varName}: ${spaceship.crew[varName].name})};

why cant I just put this ??
for(let varName in spaceship.crew){
console.log(${varName}: ${[varName].name})
}

2 Likes

because varName contains just a string, that is all it is:

for(let varName in spaceship.crew){
   console.log(varName)

so now we need to use this string to access something inside the object

5 Likes

for(let crewMember in spaceship.crew){
console.log(crewMember+’: '+spaceship.crew[crewMember].name)
}
when we are using the for loop in this way, why can’t i creat spaceship.crew.crewMember.name instead the one I put in the for loop?

for(let prop in spaceship.crew.captain){
console.log(spaceship.crew.captain.prop)
}

This for loop will out put three undefined

for(let prop in spaceship.crew.captain){
console.log(spaceship.crew.captain[prop])
}
while this will output the three correct properties of the captian

Considering that crewMember is the current object, would you not be able to write,

console.log(crewMember.name)

?

2 Likes

oh, thanks. I find the anwser on top

1 Like

if it is just a string why does varName work in the first curly brackets? I just don’t understand why you have to put spaceshp.crew[varName].name why not just put spaceship.crew.name then in this case instead of having to declare spaceship.crew to the vaiable varName

if we look at the crew object:

captain: { 
        name: 'Lily', 
        degree: 'Computer Engineering', 
        cheerTeam() { console.log('You got this!') } 
        },
    'chief officer': { 
        name: 'Dan', 
        degree: 'Aerospace Engineering', 
        agree() { console.log('I agree, captain!') } 
        },
    medic: { 
        name: 'Clementine', 
        degree: 'Physics', 
        announce() { console.log(`Jets on!`) } },
    translator: {
        name: 'Shauna', 
        degree: 'Conservation Science', 
        powerFuel() { console.log('The tank is full!') } 
        }

i don’t see crew have a name property. crew has crew members (captain, medic and so on) which we are looping over, which is why we need varName (crewMember would be a better variable name)

3 Likes

This I would like to know also. for example why cant we just put console.log( {crewMember}: {crewMember.name} );

because we write a loop:

for (let crewMember in spaceship.crew) {

JS will now assign each property of the crew object to crewMember variable.

because crewMember just contains a string, we need to use this string to access the current crew member.

2 Likes

String is a data type. Words are quite common, but a string can also be numbers:

console.log(typeof '1');

or symbols:

console.log(typeof '-+');
1 Like

so basically it is to access all the objects within the crew object by creating the crewMember. it seems like we are making a fake name to get into there

yes, the for .. in loop is doing that for us. It assigns the properties in crew object to the for loop iterator (crewMember) as string.

9 Likes

thank you for your patience and help. this was a tough one to understand!

Hello everyone! I was wondering… is it possible to iterate over the values of each keys, even if these values are associated with keys whose name vary from one another? For instance:

let spaceship = {
       color: 'gray', 
       size: 'large',
       seats: 6, 
}; 

for(let keys in spaceship){
   console.log(`The key is ${keys} and the value is ${spaceship.keys}`)
};

The output is:

The key is color and the value is undefined
The key is size and the value is undefined
The key is seats and the value is undefined```


How can I get rid of the "undefined" and be substituted by the real value assigned?

Thanks in advance!
1 Like

We cannot use dot notation on variables. They must be subscripted…

spaceship[keys]
2 Likes

Wow! Thank you, mtf! I thought the solution would be much more sophisticated! :sweat_smile:

2 Likes

Can someone please explain to me why we are able to designate a new variable (crewMember) in the loop? Why aren’t we using crew.name?

Thanks in advance!

1 Like

because this is where you declare the variable in a for in loop, then a different property name is assigned to variable (crewMember in this case) on each iteration.

because the crew object contains members, and the crew members have names.

3 Likes

Hey, I know this was some time ago but how did you resolve the “undefined” error? I am trying to do the same thing now and have tried a few things but no dice…