Accessing specific parts of objects

Tell us what’s happening:
Why is it that we need to use and . to access the online aspect of the object instead of consistently using both or both .'s. For example, why won’t usersObj.obj.online work?

Your code so far


function countOnline(usersObj) {
// Only change code below this line
let x = 0;
for (let user in usersObj){
  if(usersObj[user].online === true){
    x++;
    console.log(x);
  }
}
// Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Iterate Through the Keys of an Object with a for…in Statement

Link to the challenge:

Hello!

Let me give an example for you:
collection.prop looks for the property of a “collection” object that is specifically named “prop”.
collection[prop] looks for the property of a “collection” object that matches the value of the “prop” variable.

Does that make sense?

1 Like

ohhh. so using the period is matching the string with the property, as opposed to using the brackets is matching the variable prop. In the second case, you are calling upon a variable prop that is defined already.

sure you can, but in this case for online you need to put a string inside the brackets: usersObj[user]["online"]