I'm attempting to iterate through a bi-dimensional object to count the number of users whose online status is set to true. However, when I try to log the countOnlineUsers variable using console.log(), the count doesn't persist and appears to reset each time the loop encounters a "new" user with an "online" property.
let users = {
Alan: {
age: 27,
online: false
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: false
},
Ryan: {
age: 19,
online: true
},
George: {
age: 32,
online: true
}
};
function countOnline(obj) {
for(let user in users) {
if (!users.hasOwnProperty(user)) continue; // Initial check
let obj = users[user];
for(let prop in obj) {
if (!obj.hasOwnProperty(prop)) continue; // Secondary check
let countOnlineUsers = 0;
if(obj[prop] === true) {
countOnlineUsers++;
}
console.log(prop + " = " + obj[prop]);
console.log(countOnlineUsers);
}
}
}
console.log(countOnline(users));