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
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) {
console.log(spaceship.crew[member].name)
}
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) {
console.log(member.name)
}
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
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'){
spaceship.crew[crewMember][details]()
}
}
console.log('\n')
}
I am having quite some challenges going through this class. Could someone help me understand better? Thanks!
I am a bit lost on why there is a difference in using the dot notation and bracket notation to access the degree property for each crew member.
Below is the code and error thrown.
for (let member in spaceship.crew) {
console.log(spaceship.crew[member][degree]) // `ReferenceError: degree is not defined at Object.<anonymous> `
console.log(spaceship.crew[member].degree) // this works
Bracket Notation
In the object[expression] syntax, the expression should evaluate to a string or Symbol that represents the property’s name. So, it can be any string literal, for example, including ‘1foo’, ‘!bar!’, or even ’ ’ (a space).
// As you said this works:
console.log(spaceship.crew[member].degree)
// For bracket notation, you could do:
console.log(spaceship.crew[member]["degree"]); // This will work
console.log(spaceship["crew"][member]["degree"]); // This will work
// This won't work because crew is not in quotes
console.log(spaceship[crew][member]["degree"]);
// If you are wondering why quotes aren't needed for member,
// (let member in spaceship.crew) takes care of ensuring member is a string.
got cha! Thank you so much for the comprehensive response. Makes total sense!
In this exercise, the spaceship object contains multiple obejcts, which then contain more objects - a lot of nesting is going on.
Are the nested objects (inner objects) still properties of the outer objects? E.g.
let spaceship = {
engine: 'warp',
color: 'blue',
crew: {
captain: {
name: 'Lily',
degree: 'Computer Engineering',
cheerTeam() { console.log('You got this!') }
}
}
};
Is crew
a property of spaceship
in the same way as color
and engine
?
Also, is name
a property of captain
in the same way that captain
is a property of crew
(is there a difference between properties of outer objects and those of nested objects)?
Short answer, no. Only ‘engine’, ‘color’ and ‘crew’ are properties of spaceship
. The nested object is the value of the crew
property.
for (let crewMember in spaceship.crew) {
console.log(${crewMember}: ${spaceship.crew[crewMember].name}
)
}