Basic Data Structures: Iterate Through the Keys of an Object with a Statement Questions

Why doesn’t not work, but usersObj[user].online does?

Your code so far

function countOnline(usersObj) {
let usersOnline = 0; 
for (let user in usersObj){
      if(usersObj[user].online === true){  // won't work
  return usersOnline; 

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:71.0) Gecko/20100101 Firefox/71.0.

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

Link to the challenge:

I know it may have been a while, but there’s actually a freeCodeCamp lesson on that:

I actually looked at that, but it didn’t seem to provide any new information or clarification.

In your code user is a variable.

Doesn’t it get the property name through the for loop?

Yup and it stores it in the variable called user.

const usersOnline = {
    kso: {
        isAwesome: true
let user = "kso";
usersOnline[user]; // looks for usersOnline.kso -- the object {isAwesome: true}
usersOnline[user].isAwesome; // true
usersOnline.user; //  looks for usesrsOnline.user -- undefined
usersOnline.user.isAwesome; // throws an error

In other words in:
for (let user in userObj) {…} , I’m getting let user = “Alan”. I can’t use userObj . “Alan”. online; doesn’t work. I’ll have to use bracket notation for strings. Otherwise use dot notation like in the tutorial following this one.