Tell us what’s happening:
I understand how to access the online property through bracket notation, but I am confused as to why dot notation does not work.
I think that when using:
for( let user in users){
user.online
}
The program individually goes through each ‘user’ in users, and they all retain their properties, but when I console.log(user) only the name prints, and not the other properties; where do they go? How can I access them if I wanted to use dot notation?
For example: users.Alan.online vs for(let user in users){user.online}, where user = Alan and doesn’t seem to carry over the ‘.online’ property.
Your code so far
function countOnline(usersObj) {
// Only change code below this line
let count = 0;
for(let user in usersObj){
if(usersObj[user].online == true){
count++;
}
//if(user.online == true){count++;}
}
return count;
// 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/80.0.3987.149 Safari/537.36
.
Challenge: Iterate Through the Keys of an Object with a for…in Statement
Link to the challenge: