FAQ: Objects - Looping Through Objects


#22

I think that because dot notations looks for Keys? and keys only?


#23

i think that, what does that refer to? You use it, but without context its difficult to determine what you mean


#24

I think the reason why you have to put [position] inside bracket notations is because it is a variable and not a property right?


#25

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']

#26

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})
}


#27

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


#28

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


#29

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

console.log(crewMember.name)

?


#30

oh, thanks. I find the anwser on top


#31

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


#32

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)


#33

I kind of understand better but not completely. why do we need varName or crewMember ? and also I still don’t understand why we have to put spaceship.crew[crewMember].name instead of just [crewMember].name ?

for example why cant we just put console.log({crewMember}: {crewMember.name});


#34

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


#35

you see one works and the other dont! I am asking why do we need to put the spaceship.crew before? and why does the first varName work but not the second one?

doeswork

dontwork


#36

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.


#37

what does string mean? I thought a string is words within quotations


#38

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 '-+');

#39

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


#40

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.


#41

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