Using FOR..IN to loop through objects

Using FOR..IN to loop through objects
0.0 0

#1

Please help me understand what is happening here:

let users = {
  Alan: {
    age: 27,
    online: false
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: false
  },
  Ryan: {
    age: 19,
    online: true
  }
};

function countOnline(obj) {
  // change code below this line
for (let usersOnline in obj) {
  if (usersOnline[online] == true) {
    return true;
  }
}
return false;
  // change code above this line
}

console.log(countOnline(users));

What I want to do here is return true if Alan, Jeff, Sarah, Ryan, 's online status is true.

But the results says that online is not defined. Why is that?


#2

Because dot notation and brackets notation are not the same, this article explains very well why ! :slightly_smiling_face:

To get your example working, just replace usersOnline[online] by usersOnline.online


#3

Thank you for the article @thomlom. exactly the answer I needed.